+ /* Create the socket */
+ sck->m_fd = socket(sck->m_peer->m_realfamily,
+ sck->m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
+
+ if (sck->m_fd == INVALID_SOCKET)
+ {
+ sck->m_error = GSOCK_IOERR;
+ return GSOCK_IOERR;
+ }
+
+ ioctl(sck->m_fd, FIONBIO, &arg);
+ _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)
+ {
+ close(sck->m_fd);
+ sck->m_fd = INVALID_SOCKET;
+ /* 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;
+ }