title: C++ winsock connection refused when connecting to IPv4 address
body: <p>I'm programming a server/client system using winsock2 and it works great when I connect the client to the <strong>server name</strong> or the <strong>server IPv6 address</strong>. However, when I use the <strong>server IPv4 address</strong> I get error "Connection refused" from the call to connect() in the client.</p>
  
  <p>This error occurs with either my client or using telnet. However, I can successfully ping the server using either of the three name, IPv4 or IPv6.</p>
  
  <p>I've tried this running both server and client on the same machine, on separate machines, and firewalls deactivated on all machines.</p>
  
  <p>Here is an excerpt of my server initialization and listening code:</p>
  
  <pre><code>SOCKET sockfd = INVALID_SOCKET, in_socketID;
  struct addrinfo hints;
  struct addrinfo *servinfo = NULL;
  struct addrinfo *p;
  struct addrinfo *ip;
  sockaddr_storage incoming_addr;
  int addr_size;
  int tmp_err;
  const char *sPort = "20152";
  
  memset(&amp;hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC; // either IPv4 or IPv6
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  
  tmp_err = getaddrinfo(NULL, sPort, &amp;hints, &amp;servinfo);
  if (tmp_err != 0)
      throw exception("ERROR: getaddrinfo failed");
  
  // loop through all the results and bind to the first we can
  for(p = servinfo; p != NULL &amp;&amp; sockfd == INVALID_SOCKET; p = p-&gt;ai_next)
  {
      ip = p;
      sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol);
      if (sockfd == INVALID_SOCKET)
      {
          cerr &lt;&lt; "ERROR on socket(): " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      } // end if
      else if (bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == SOCKET_ERROR)
      {
          cerr &lt;&lt; "ERROR on bind(): " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          closesocket(sockfd);
          sockfd = INVALID_SOCKET;
      } // end if
  } // end for
  
  if (sockfd == INVALID_SOCKET)
  {
      // looped off the end of the list with no successful bind
      throw exception("ERROR: Failed to bind socket");
  }
  
  // clean up
  if (servinfo)
      freeaddrinfo(servinfo);
  
  if (listen(sockfd, SOMAXCONN ) == SOCKET_ERROR)
      throw exception("Listen failed");
  
  while (true)
  {
      memset(&amp;incoming_addr, 0, sizeof(incoming_addr));
      addr_size = sizeof(incoming_addr);
      in_socketID = accept(socketID, (sockaddr *)&amp;incoming_addr, &amp;addr_size);
  
      // do stuff with incoming connection
  }
  </code></pre>
  
  <p>This is my client code:</p>
  
  <pre><code>int sockfd = INVALID_SOCKET;
  struct addrinfo hints;
  struct addrinfo *servinfo = NULL;
  struct addrinfo *p;
  struct addrinfo *ip;
  int tmp_err;
  const char *sHost = "192.168.1.136";
  const char *sPort = "20152";
  
  memset(&amp;hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC; // either IPv4 or IPv6
  hints.ai_socktype = SOCK_STREAM; // use TCP
  
  tmp_err = getaddrinfo(sHost,        // web address or ip to connect to
                        sPort,        // port or protocol
                        &amp;hints,       // initialized hints structure
                        &amp;servinfo);   // return structure
  if (tmp_err != 0)
      throw exception("ERROR: getaddrinfo failed");
  
  // loop through all the results and connect to the first we can
  for(p = servinfo; p != NULL &amp;&amp; sockfd == INVALID_SOCKET; p = p-&gt;ai_next)
  {
      ip = p;
      sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol);
      if (sockfd == INVALID_SOCKET)
      {
          cerr &lt;&lt; "ERROR on socket(): " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          //continue;
      } // end if
      else if (connect(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) &lt; 0)
      {
          cerr &lt;&lt; "ERROR on connect(): " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          closesocket(sockfd);
          sockfd = INVALID_SOCKET;
          //continue;
      } // end if
  } // end for
  
  if (sockfd == INVALID_SOCKET)
      throw exception("ERROR: Failed to connect");
  
  
  // clean up
  if (servinfo)
      freeaddrinfo(servinfo);
  
  // do stuff with new socket
  </code></pre>
  
  <p>I already read several similar questions in the site, but none answered this issue.</p>
  
  <p>How can I connect also to the server IPv4 address? I need help, please.</p>
  
  <p>Thanks.</p>
  
  <h2>EDIT:</h2>
  
  <p>From a suggestion given by user Sorayuki, I made some changes just to test if his theory was correct.</p>
  
  <p>I was able to connect to the IPv4 by changing on the server</p>
  
  <pre><code>hints.ai_family = AF_UNSPEC;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>hints.ai_family = AF_INET;
  </code></pre>
  
  <p>I knew it would obviously work, but when I do this, of course IPv6 doesn't work.</p>
  
  <p>It appears user Sorayuki was right and my loop was connecting to IPv6.</p>
  
  <p>It seems that there is no easy way to unify IPv6 and IPv4. Your socket must listen to either one or the other which makes the process really annoying.</p>
  
  <p>According to the documentation, the old style to listen to both IPv4 and IPv6 is to create a socket for each and listen on both. This is for Windows Server 2003 and Windows XP SP1.</p>
  
  <p>The preferred modern style (Windows Vista, 7 and 8) is to turn your socket into a dual socket and it will listen to both IPv4 and IPv6. However, your client must also be able to set up a dual socket, so, if your application is serving an old client, you are stuck with the old method.</p>
  
  <p>Thanks!</p>