title: TCP nonblock connect failed with getsockopt OptValue 113 Please suggest
body: <p>I have created a sample TCP connect function. This uses non-blocking socket with timeout. The code is working fine in our lab but in some network TCP connect fails with error
  115 which means EINPROGRESS. After debug found <code>getsockopt()</code> set the iValopt to 113
  and we check <code>optvalue</code> is equal to zero, if not return failure.</p>
  
  <p>OS: LINUX Suse10 </p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Is the code pasted here right?</li>
  <li>Why <code>getsockopt()</code> Opt value is set to 113 and errno set to 115?</li>
  </ol>
  
  <p>Also I notice below.</p>
  
  <ul>
  <li>Client has multiple IP:  174.66.45.22, 58.68.445.112</li>
  <li>Server has multiple IP:  174.88.69.33, 58.46.22.33</li>
  <li>If I set Bind address to 174.66.45.22 and connect to 174.88.69.33 is always success.</li>
  <li>If I set Bind address to 174.66.45.22 and connect to 58.46.22.33 will fail. </li>
  <li>But if I just set Bind address to 58.68.445.112 in first run and in second run I change Bind address to 174.66.45.22 then connect to 58.46.22.33 is Success!</li>
  </ul>
  
  <p>Can you explain why?</p>
  
  <p>The exact code I have pasted here</p>
  
  <pre><code>int tcp_sockconnect_linux(int iSocket, const struct sockaddr* pstSockAddr, unsigned int uiSockAddrLen, unsigned int uiConnectionTimeout)
  {
      int iRet  = 0;
      //int iValopt = 0;
      int iLength = 0; 
      struct timeval stTv; 
      fd_set write_fds;
  
      (void)fcntl((int)(long)iSocket, F_SETFL,  O_NONBLOCK);
  
      iRet = connect((int)(long)iSocket, (struct sockaddr*) pstSockAddr, uiSockAddrLen); 
  
      if (iRet &lt; 0)
      { 
          if (errno == EINPROGRESS) 
          { 
              stTv.tv_sec = uiConnectionTimeout; 
              stTv.tv_usec = 0; 
              FD_ZERO(&amp;write_fds); 
              FD_SET((int)(long)iSocket, &amp;write_fds); 
  
              if (0 &lt; select(((int)(long)(iSocket)) + 1,NULL, &amp;write_fds, NULL, &amp;stTv))
              { 
                  iLength = sizeof(int); 
  
                  if (0 &gt; getsockopt((int)(long)iSocket, SOL_SOCKET,SO_ERROR, (void*)(&amp;iValopt), &amp;iLength))
                  {
                  return -1;
                  }
  
                  if (0 != iValopt)
                  { 
                  return -1;
                  }
  
                  return 2;
              } 
              else
              { 
              return -1; 
              } 
          } 
          else
          { 
          return -1; 
          } 
      } 
  
      return 2;
  }
  
  
  int create_socket(void) 
  {
      int iRet, sock_sd; 
      struct sockaddr_in saServer;
      struct sockaddr_in saLocal;     
  
  #ifdef WIN32
      WSADATA wsaData;
      if(WSAStartup(0x101, &amp;wsaData)) 
      {
          printf("Unable to initialize WinSock library.\n");
      }
  #endif
  
      memset (&amp;saLocal,'\0', sizeof(saLocal));  
      saLocal.sin_family      = AF_INET;
      saLocal.sin_addr.s_addr = inet_addr(strBindAddr);
      saLocal.sin_port        = htons(0);
  
      sock_sd = socket(AF_INET,SOCK_STREAM,0);
      memset (&amp;saServer,'\0', sizeof(saServer));  
      saServer.sin_family      = AF_INET;
      saServer.sin_addr.s_addr = inet_addr (strServerIP); 
      saServer.sin_port        = htons(uiServerPort);  
  
      iRet = bind((unsigned int)sock_sd,(struct sockaddr*) &amp;saLocal,sizeof(saLocal));
  #ifdef WIN32
      if (iRet ==  SOCKET_ERROR)
      {
          printf("\nTCP Bind Failed to LocalIP %s\n",strBindAddr);
      }
  #else
      if (-1 == iRet)
      {
          printf("\nTCP Bind Failed to LocalIP %s\n",strBindAddr);
      }
  #endif
  
  
  
  #ifdef WIN32
      iRet = tcp_sockconnect_win(sock_sd, (struct sockaddr*) &amp;saServer,sizeof(saServer),uiConnectionTimeOut); 
      if (iRet ==  SOCKET_ERROR)
      {
          printf("\nTCP Socket Connect Failed with  ErrorNo = %d  iValopt = %d  LineNo = %d \n",WSAGetLastError(),iValopt,iLineNo);
      }
      else if (2 == iRet)
      {
          printf("\nTCP Socket Connect Success \n");
      }
  #else
      iRet = tcp_sockconnect_linux(sock_sd, (struct sockaddr*) &amp;saServer,sizeof(saServer),uiConnectionTimeOut);
      if(-1 == iRet)
      {
          printf("\nTCP Socket Connect Failed with  ErrorNo = %d   iValopt = %d  LineNo = %d\n",errno,iValopt,iLineNo);
      }
      else if (2 == iRet)
  
      {
          printf("\nTCP Socket Connect Success %d\n",iLineNo);
      }
  
  #endif
  
  
  #ifdef WIN32
      closesocket (sock_sd);
  #else
      close(sock_sd);
  #endif
  
      return 0;
  }
  
  int main ()
  {
      int iRet = 0;
  
      printf ("Enter the Bind Address (Local IP): ");
      scanf("%s" ,&amp;strBindAddr);
  
      printf ("\nEnter the IP Address of Server : ");
      scanf("%s" ,&amp;strServerIP);
  
      printf ("\nEnter the PORT Number of Server : ");
      scanf("%d" ,&amp;uiServerPort);
  
      printf ("\nEnter the Connection timeout in Seconds : ");
      scanf("%d", &amp;uiConnectionTimeOut);
  
      iRet = create_socket();
  
      return 0;
  }
  </code></pre>