Index: pjproject/trunk/pjlib/src/pj/ioqueue_winnt.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/ioqueue_winnt.c	(revision 71)
+++ pjproject/trunk/pjlib/src/pj/ioqueue_winnt.c	(revision 83)
@@ -203,38 +203,59 @@
  * code.
  */
-static pj_ioqueue_key_t *check_connecting( pj_ioqueue_t *ioqueue, 
-					   pj_ssize_t *connect_err )
-{
-    pj_ioqueue_key_t *key = NULL;
-
+static int check_connecting( pj_ioqueue_t *ioqueue )
+{
     if (ioqueue->connecting_count) {
-	DWORD result;
+	int i, count;
+	struct 
+	{
+	    pj_ioqueue_key_t *key;
+	    pj_status_t	      status;
+	} events[PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL-1];
 
 	pj_lock_acquire(ioqueue->lock);
-	result = WaitForMultipleObjects(ioqueue->connecting_count,
-					ioqueue->connecting_handles,
-					FALSE, 0);
-	if (result >= WAIT_OBJECT_0 && 
-	    result < WAIT_OBJECT_0+ioqueue->connecting_count) 
-	{
-	    WSANETWORKEVENTS net_events;
-
-	    /* Got completed connect(). */
-	    unsigned pos = result - WAIT_OBJECT_0;
-	    key = ioqueue->connecting_keys[pos];
-
-	    /* See whether connect has succeeded. */
-	    WSAEnumNetworkEvents((pj_sock_t)key->hnd, 
-				 ioqueue->connecting_handles[pos], 
-				 &net_events);
-	    *connect_err = 
-                PJ_STATUS_FROM_OS(net_events.iErrorCode[FD_CONNECT_BIT]);
-
-	    /* Erase socket from pending connect. */
-	    erase_connecting_socket(ioqueue, pos);
+	for (count=0; count<PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL-1; ++count) {
+	    DWORD result;
+
+	    result = WaitForMultipleObjects(ioqueue->connecting_count,
+					    ioqueue->connecting_handles,
+					    FALSE, 0);
+	    if (result >= WAIT_OBJECT_0 && 
+		result < WAIT_OBJECT_0+ioqueue->connecting_count) 
+	    {
+		WSANETWORKEVENTS net_events;
+
+		/* Got completed connect(). */
+		unsigned pos = result - WAIT_OBJECT_0;
+		events[count].key = ioqueue->connecting_keys[pos];
+
+		/* See whether connect has succeeded. */
+		WSAEnumNetworkEvents((pj_sock_t)events[count].key->hnd, 
+				     ioqueue->connecting_handles[pos], 
+				     &net_events);
+		events[count].status = 
+		    PJ_STATUS_FROM_OS(net_events.iErrorCode[FD_CONNECT_BIT]);
+
+		/* Erase socket from pending connect. */
+		erase_connecting_socket(ioqueue, pos);
+	    } else {
+		/* No more events */
+		break;
+	    }
 	}
 	pj_lock_release(ioqueue->lock);
-    }
-    return key;
+
+	/* Call callbacks. */
+	for (i=0; i<count; ++i) {
+	    if (events[i].key->cb.on_connect_complete) {
+		events[i].key->cb.on_connect_complete(events[i].key, 
+						      events[i].status);
+	    }
+	}
+
+	return count;
+    }
+
+    return 0;
+    
 }
 #endif
@@ -439,6 +460,7 @@
     generic_overlapped *pOv;
     pj_ioqueue_key_t *key;
+    int connect_count;
     pj_ssize_t size_status = -1;
-    BOOL rc;
+    BOOL rcGetQueued;;
 
     PJ_ASSERT_RETURN(ioqueue, -PJ_EINVAL);
@@ -446,9 +468,5 @@
     /* Check the connecting array. */
 #if PJ_HAS_TCP
-    key = check_connecting(ioqueue, &size_status);
-    if (key != NULL) {
-	key->cb.on_connect_complete(key, (int)size_status);
-	return 1;
-    }
+    connect_count = check_connecting(ioqueue);
 #endif
 
@@ -457,6 +475,7 @@
 
     /* Poll for completion status. */
-    rc = GetQueuedCompletionStatus(ioqueue->iocp, &dwBytesTransfered, &dwKey,
-				   (OVERLAPPED**)&pOv, dwMsec);
+    rcGetQueued = GetQueuedCompletionStatus(ioqueue->iocp, &dwBytesTransfered,
+					    &dwKey, (OVERLAPPED**)&pOv, 
+					    dwMsec);
 
     /* The return value is:
@@ -504,20 +523,9 @@
 	    break;
 	}
-	return 1;
-    }
-
-    if (GetLastError()==WAIT_TIMEOUT) {
-	/* Check the connecting array (again). */
-#if PJ_HAS_TCP
-	size_status = -1;   /* make MSVC happy */
-	key = check_connecting(ioqueue, &size_status);
-	if (key != NULL) {
-	    key->cb.on_connect_complete(key, (int)size_status);
-	    return 1;
-	}
-#endif
-	return 0;
-    }
-    return -1;
+	return connect_count+1;
+    }
+
+    /* No event was queued. */
+    return connect_count;
 }
 
