diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index ccb8b86..9efc9fa 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2236,6 +2236,20 @@ ClosePostmasterPorts(bool am_syslogger)
 			StreamClose(ListenSocket[i]);
 			ListenSocket[i] = PGINVALID_SOCKET;
 		}
+        else
+		{
+			/*
+			 * Do not process the rest of the array elements since we expect
+			 * the presence of an invalid socket id to mark the end of valid
+			 * elements.
+			 */
+#ifdef USE_ASSERT_CHECKING
+			int j;
+			for(j = i; j < MAXLISTEN; j++)
+				Assert(ListenSocket[i] == PGINVALID_SOCKET);
+#endif
+            break;
+		}
 	}
 
 	/* If using syslogger, close the read side of the pipe */
