title: In a non blocking socket connect, select() always returns 1
body: <p>I have this code segment that is designed to connect to a server using a socket connection. However if it can not connect to the server within a certain amount of time I would like it to stop trying. I tried to do this with this nonblocking socket and the select command but select is always returning 1 indicating that the server exists when nothing exists at the address I give it. Any Ideas?</p>
  
  <pre><code>SOCKET tcp_client( char *hname, char *sname )  {    
      fd_set fdset;
      struct sockaddr_in peer;
      SOCKET s;
      FD_ZERO(&amp;fdset);
      // FD_SET(STDIN, &amp;fdset);
      FD_SET(s, &amp;fdset);
      errno=1;
      struct timeval tv;
      tv.tv_sec = 15;
      set_address( hname, sname, &amp;peer, "tcp" );
      s = socket( AF_INET, SOCK_STREAM, 0 );
  
      int n = 1;
      fcntl(s, F_SETFL, O_NONBLOCK);
  
      if ( !isvalidsock( s ) )
      {
          printf("Socket Call Failed: %s\n", strerror(errno));
          return(0);
      }
  
      int x = 0;
  
      int status = connect( s, ( struct sockaddr * )&amp;peer, sizeof( peer ) );
  
      if(status &lt; 0) {
          printf("Status: %i\n", status); 
      }
  
      int retVal = select(s+1, &amp;fdset, NULL, NULL, &amp;tv);
      printf("retVal: %i\n", retVal);
  
      if (retVal == 1) {
          int so_error;
          socklen_t slen = sizeof so_error;
          getsockopt(s, SOL_SOCKET, SO_ERROR, &amp;so_error, &amp;slen);
          if (so_error == 0) {
              printf("work\n");
  
              x =1;
          } else {
              printf("fail\n");
              x = 0;
          }
      } else {     
          printf("noSocks\n"); 
      }
  
  
      if (x ==0 )
      {
          printf("Connect Failed: %s\n", strerror(errno));
          L("libOnexc: Connect to socket failed");
          close(s);
          return(0);
      } 
  
      return s;
  }
  </code></pre>