Index: sphinxclient.c =================================================================== --- sphinxclient.c (revision 20) +++ sphinxclient.c (working copy) @@ -1221,7 +1221,7 @@ struct hostent * hp; struct sockaddr_in sa; struct timeval timeout; - fd_set fds_write; + fd_set fds_write, fds_read; int sock, to_wait, res; hp = gethostbyname ( client->host ); @@ -1252,7 +1252,7 @@ res = connect ( sock, (struct sockaddr*)&sa, sizeof(sa) ); if ( res==0 ) return sock; - if ( sock_errno()!=EWOULDBLOCK ) + if ( sock_errno()!=EWOULDBLOCK && sock_errno() != EINPROGRESS) { set_error ( client, "connect() failed: %s", sock_error() ); return -1; @@ -1264,9 +1264,21 @@ timeout.tv_usec = ( to_wait % 1000 ) * 1000; // remainder is msec, so *1000 for usec FD_ZERO ( &fds_write ); SPH_FD_SET ( sock, &fds_write ); + SPH_FD_SET ( sock, &fds_read ); - res = select ( 1, NULL, &fds_write, NULL, &timeout ); + res = select ( sock + 1, &fds_read, &fds_write, NULL, &timeout ); + if (res > 0) { + int error; + socklen_t len = sizeof(error); + + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) { + res = -1; + } else { + res = 0; + } + } + if ( res>=0 && FD_ISSET ( sock, &fds_write ) ) return sock;