title: Non blocking connect call doesn't return connection refused
body: <p>I have setup a timeout on a non-blocking connect call, which timesout correctly when the connection is attempted to an address that will not respond.
  However, when the connection is refused by the destination, it does not seem to return the connection refused call.</p>
  
  <pre><code>int x = fcntl(iSock,F_GETFL, 0);
  fcntl(iSock,F_SETFL, x | O_NONBLOCK);
  
  fd_set writeFD;
  
  FD_ZERO(&amp;writeFD);
  FD_SET(iSock, &amp;writeFD);
  
  timeval timeout;
  timeout.tv_sec = 30;
  timeout.tv_usec = 0;
  
  errno = 0;
  
  if ((iStat = connect(iSock, (struct sockaddr *)&amp;addr, sizeof(addr))) &lt; 0)
  {
      if(errno == EINPROGRESS)
      {
          if(int retval = (select (iSock+1, (fd_set *)NULL, &amp;writeFD, (fd_set *)NULL, (struct timeval *)(&amp;timeout))  &gt; 0) )
          {
              socklen_t len = 0;
              int error = 0;
              if (getsockopt(iSock, SOL_SOCKET, SO_ERROR, &amp;error, &amp;len) &lt; 0)
                  return (-1);
  
              if (error == EINPROGRESS)
              {
                  close(iSock);
                  errno = error;
                  return (-1);
              }
              else
              {
                  printf("Connected \n");
                  return 0;
              }
          }
          else
          {
              printf("Not Connected - %d\n", errno);
              close(iSock);
              return iStat;
          }
      }
      else
      {
          close(iSock);
          return iStat;
      }
  }
  </code></pre>
  
  <p>From the connect call, it always seems to return EINPROGRESS, then the select call will return > 0 with error set as 0.</p>
  
  <p>When I change this to a blocking connect call, I get the return code of CONNECTIONREFUSED immediately from the connect call.</p>