+ if (sck->m_fd == INVALID_SOCKET)
+ {
+ sck->m_error = GSOCK_IOERR;
+ return GSOCK_IOERR;
+ }
+#if defined(__EMX__) || defined(__VISAGECPP__)
+ ioctl(sck->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
+#else
+ ioctl(sck->m_fd, FIONBIO, &arg);
+#endif
+ _GSocket_Enable_Events(sck);
+
+ /* Connect it to the peer address, with a timeout (see below) */
+ ret = connect(sck->m_fd, sck->m_peer->m_addr, sck->m_peer->m_len);
+
+ if (ret == -1)
+ {
+ err = errno;
+
+ /* If connect failed with EINPROGRESS and the GSocket object
+ * is in blocking mode, we select() for the specified timeout
+ * checking for writability to see if the connection request
+ * completes.
+ */
+ if ((err == EINPROGRESS) && (!sck->m_non_blocking))
+ {
+ if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)
+ {
+ GSocket_close(sck);
+ /* sck->m_error is set in _GSocket_Output_Timeout */
+ return GSOCK_TIMEDOUT;
+ }
+ else
+ {
+ int error;
+ SOCKLEN_T len = sizeof(error);
+
+ getsockopt(sck->m_fd, SOL_SOCKET, SO_ERROR, (void*) &error, &len);
+
+ if (!error)
+ return GSOCK_NOERROR;
+ }
+ }
+
+ /* If connect failed with EINPROGRESS and the GSocket object
+ * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
+ * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
+ * this way if the connection completes, a GSOCK_CONNECTION
+ * event will be generated, if enabled.
+ */
+ if ((err == EINPROGRESS) && (sck->m_non_blocking))
+ {
+ sck->m_establishing = TRUE;
+ sck->m_error = GSOCK_WOULDBLOCK;
+ return GSOCK_WOULDBLOCK;
+ }
+
+ /* If connect failed with an error other than EINPROGRESS,
+ * then the call to GSocket_Connect has failed.
+ */
+ GSocket_close(sck);