title: setsockopt does not work in C++ (timeout set to 1s, it waits longer)
body: <p>I want to build a simple port scanner in C++. However, setting the timeout of the socket to 1s, the program waits much longer before testing the next port. Am I doing something wrong?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fstream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/time.h&gt;
  
  int main()
  {
      int socketFileDescriptor;
      struct sockaddr_in server_address;
  
      for (int i=3000; i&lt;65535; i++)
      {
          std::cout&lt;&lt;i&lt;&lt;std::endl;
  
          socketFileDescriptor = socket(AF_INET,SOCK_STREAM, 0);
  
          struct timeval tv;
          tv.tv_sec = 1;
  
          if (setsockopt(socketFileDescriptor, SOL_SOCKET, SO_RCVTIMEO, &amp;tv, sizeof(tv)) &lt; 0)
          {
              perror("Error");
          }
  
          server_address.sin_family = AF_INET;
          server_address.sin_port = htons(i);
          inet_pton(AF_INET,"212.182.24.27", &amp;(server_address.sin_addr));
  
          int result = connect(socketFileDescriptor,
                               (struct sockaddr*)&amp;server_address,sizeof(struct sockaddr));
          if (result != -1)
          {
              std::cout&lt;&lt;"Port: "&lt;&lt;i&lt;&lt;std::endl;
          }
          close(socketFileDescriptor);
      }
  }
  </code></pre>