while ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1) { rv = WSAGetLastError(); if ((!fd->secret->nonblocking) && ((rv == WSAEWOULDBLOCK) || (rv == WSAEALREADY) || (rv == WSAEINVAL) )) { if (timeout == PR_INTERVAL_NO_TIMEOUT) tvp = NULL; else { tv.tv_sec = PR_IntervalToSeconds(timeout); tv.tv_usec = PR_IntervalToMicroseconds( timeout - PR_SecondsToInterval(tv.tv_sec)); tvp = &tv; }  FD_ZERO(&wd); FD_SET((SOCKET)osfd, &wd); rv = select(osfd + 1, NULL, &wd, NULL, tvp); if (rv > 0) {  Sleep(0); } else if (rv == 0) { PR_SetError(PR_IO_TIMEOUT_ERROR, 0); return(-1); } else if (rv < 0) { rv = WSAGetLastError(); _PR_MD_MAP_SELECT_ERROR(rv); return(-1); } } else if ((rv == WSAEISCONN)) {  return 0; } else { _PR_MD_MAP_CONNECT_ERROR(rv); return -1; }
if ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1) { err = WSAGetLastError(); _PR_MD_MAP_CONNECT_ERROR(err);
_PR_MD_CONNECT(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen,                PRIntervalTime timeout){    PRInt32 osfd = fd->secret->md.osfd;    PRThread *me = _PR_MD_CURRENT_THREAD();    PRInt32 rv;    PRThread *cThread;    struct connect_data_s cd;    if (fd->secret->nonblocking) {        PRInt32 rv;        fd_set wd;        struct timeval tv, *tvp;        if (!fd->secret->md.io_model_committed) {            rv = _md_MakeNonblock((HANDLE)osfd);            PR_ASSERT(0 != rv);            fd->secret->md.io_model_committed = PR_TRUE;        }        while ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1) {            rv = WSAGetLastError();            if ((!fd->secret->nonblocking) && ((rv == WSAEWOULDBLOCK) ||                (rv == WSAEALREADY) ||                (rv == WSAEINVAL) /* for winsock1.1, it reports EALREADY as EINVAL */)) {                if (timeout == PR_INTERVAL_NO_TIMEOUT)                    tvp = NULL;                else {                    tv.tv_sec = PR_IntervalToSeconds(timeout);                    tv.tv_usec = PR_IntervalToMicroseconds(                    timeout - PR_SecondsToInterval(tv.tv_sec));                    tvp = &tv;                }                FD_ZERO(&wd);                FD_SET((SOCKET)osfd, &wd);                rv = select(osfd + 1, NULL, &wd, NULL, tvp);                if (rv > 0) {                    /*                     * Call Sleep(0) to work around a Winsock timing bug.                     */                    Sleep(0);                } else if (rv == 0) {					PR_SetError(PR_IO_TIMEOUT_ERROR, 0);                    return(-1);                } else if (rv < 0) {                    rv = WSAGetLastError();            		_PR_MD_MAP_SELECT_ERROR(rv);                    return(-1);                }            } else if ((rv == WSAEISCONN)) {                /* Success! */                return 0;            } else {                _PR_MD_MAP_CONNECT_ERROR(rv);                return -1;            }        }        return rv;    }    /* If we are a native thread, just make the blocking IO call */    if (_PR_IS_NATIVE_THREAD(me)) {        rv = connect(osfd, (struct sockaddr *)addr, addrlen);		if (rv == -1) {        	rv = WSAGetLastError();            _PR_MD_MAP_CONNECT_ERROR(rv);			return -1;		} else			return rv;    }    /* NT doesn't provide a nice way to do asynchronous      * connect.  The proxy team invented a huge chunk of code which has     * a single thread multiplexing multiple connect requests via      * WSAAsyncSelect().  That is a better solution, but I'm not doing that     * now.  At this point, just create a real thread to do the work.     *     * Rumor has it that on nt3.51, all the WSA library does is create      * a thread to call a blocking connect() anyway.  On 4.0 they've fixed     * that.  -mbelshe     */    cd.osfd = osfd;    cd.addr = (struct sockaddr *)addr;    cd.addrlen = addrlen;    cd.timeout = timeout;    cThread = PR_CreateThread(PR_SYSTEM_THREAD,                              _PR_MD_connect_thread,                              (void *)&cd,                              PR_PRIORITY_NORMAL,                              PR_GLOBAL_THREAD,                              PR_JOINABLE_THREAD,                              0);    if (cThread == NULL) {        return -1;    }    PR_JoinThread(cThread);    rv = cd.status;    if (rv == SOCKET_ERROR) {        _PR_MD_MAP_CONNECT_ERROR(cd.error);        return -1;    }    return 0;}