title: issue in non-blocking connect() with sockets in C
body: <p>I am currently studying Non-blocking I/O in socket programming. Within this topic, I came across the concept where one can make connect() as non-blocking i.e. if the connection is still not established, it would immediately return EINPROGRESS error. Meanwhile the connection is getting established user can perform some other tasks as stated in the book too which is as follow: </p>
  
  <blockquote>
    <p>There are three uses for a nonblocking connect:</p>
    
    <ol>
    <li><p>We can overlap other processing with the three-way handshake. A
    connect takes one RTT to complete (Section 2.6) and this can be
    anywhere from a few milliseconds on a LAN to hundreds of milliseconds
    or a few seconds on a WAN. There might be other processing we wish to
    perform during this time.</p></li>
    <li><p>We can establish multiple connections at the same time using this
    technique. This has become popular with Web browsers, and we will show
    an example of this in Section 16.5.</p></li>
    <li><p>Since we wait for the connection to be established using select, we
    can specify a time limit for select, allowing us to shorten the
    timeout for the connect. Many implementations have a timeout for
    connect that is between 75 seconds and several minutes. There are
    times when an application wants a shorter timeout, and using a
    nonblocking connect is one way to accomplish this. </p></li>
    </ol>
  </blockquote>
  
  <p>I have a doubt in the code that is mentioned in the book in the support of the same. The code is as follow : </p>
  
  <pre><code> 1 #include     "unp.h" // a header file containing all necessary files required to run this program   
   2 int
   3 connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec)
   4 {
   5     int     flags, n, error;
   6     socklen_t len;
   7     fd_set rset, wset;
   8     struct timeval tval;
  
   9     flags = Fcntl(sockfd, F_GETFL, 0);
  10     Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  
  11     error = 0;
  12     if ( (n = connect(sockfd, saptr, salen)) &lt; 0)
  13         if (errno != EINPROGRESS)
  14             return (-1);
  
  15     /* Do whatever we want while the connect is taking place. */
  
  16     if (n == 0)
  17         goto done;               /* connect completed immediately */
  
  18     FD_ZERO(&amp;rset);
  19     FD_SET(sockfd, &amp;rset);
  20     wset = rset;
  21     tval.tv_sec = nsec;
  22     tval.tv_usec = 0;
  
  23     if ( (n = Select(sockfd + 1, &amp;rset, &amp;wset, NULL,
  24                     nsec ? &amp;tval : NULL)) == 0) {
  25         close(sockfd);          /* timeout */
  26         errno = ETIMEDOUT;
  27         return (-1);
  28     }
  
  29     if (FD_ISSET(sockfd, &amp;rset) || FD_ISSET(sockfd, &amp;wset)) {
  30         len = sizeof(error);
  31         if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &amp;error, &amp;len) &lt; 0)
  32             return (-1);     /* Solaris pending error */
  33     } else
  34         err_quit("select error: sockfd not set");
  
  35   done:
  36     Fcntl(sockfd, F_SETFL, flags);  /* restore file status flags */
  
  37     if (error) {
  38         close(sockfd);           /* just in case */
  39         errno = error;
  40         return (-1);
  41     }
  42     return (0);
  43 }
  </code></pre>
  
  <p>The code above is the wrapper function for the connect() utility supported by sockets API. Here we have made the socket as non-blocking. </p>
  
  <p>The <strong>doubt</strong> is that if the connection is not yet established and as per line 15 we can proceed with some other task which is mentioned in the comment.  But shouldn't the immediate connection condition be first checked . Since without checking that one we proceeded with do whatever which will take time when the connection could have happened before.</p>
  
  <p>Please correct me if I am wrong. </p>