title: hostname not translated into an IP address using Winsock
body: <p><code>getaddrinfo()</code> does not translate a hostname into an IP address and consequently does not <code>connect()</code> to the server. Is something wrong with my implementation - compiles with no warning messages?</p>
  
  <p>Is this function call to <code>connect</code> correct?</p>
  
  <pre><code>connect(client, result-&gt;ai_addr, result-&gt;ai_addrlen)
  </code></pre>
  
  <p>Full implementation listed below:</p>
  
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  using namespace std;
  
  int main (
            int argc,
            char* argv[])
  {
  if (argc != 3)
  {
      cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " [hostname] [port number]\n";
      exit(EXIT_FAILURE);
  }
  
  WSADATA wsaData;
  WORD wVersionRequested;
  int wError;
  
  wVersionRequested = MAKEWORD(2, 2);
  
  wError = WSAStartup(wVersionRequested, &amp;wsaData);
  
  if (wError != 0)
  {
      cerr &lt;&lt; "WSAStartup failed with error: " &lt;&lt; wError &lt;&lt; endl;
      exit (EXIT_FAILURE);
  }
  
  /*
  * Confirm that the WinSock DLL supports 2.2.
  * Note that if the DLL supports versions greater
  * than 2.2 in addition to 2.2, it will still return 
  * 2.2 in wVersion since that is the version we 
  * requested. 
  */
  
  if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
  {
      cerr &lt;&lt; "Could not find a usable version of Winsock.dll." &lt;&lt; endl;
      WSACleanup();
      exit(EXIT_FAILURE);
  } else {
      cout &lt;&lt; "The Winsock 2.2 dll was found." &lt;&lt; endl;
  }
  
  SOCKET client;
  
  if ((client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
  {
      cerr &lt;&lt; "Error: socket() return value == SOCKET_ERROR" &lt;&lt; endl;
      WSACleanup();
      exit (EXIT_FAILURE);
  }
  
  cout &lt;&lt; "Created a socket." &lt;&lt; endl;
  
  struct addrinfo *result = NULL;
  struct addrinfo hints;
  memset(&amp;hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  
  if ((wError = getaddrinfo(
                      argv[1],
                      argv[2],
                      &amp;hints,
                      &amp;result)) !=0 )
  {
      freeaddrinfo(result);
      WSACleanup();
      if (wError == 11001)
      {
          cerr &lt;&lt; "Error: occurred: getaddrinfo() failed "
               &lt;&lt; wError &lt;&lt; " - Host not found." &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
  
      cerr &lt;&lt; "Error: occurred: getaddrinfo() failed "
              &lt;&lt; wError &lt;&lt; endl;
      exit(EXIT_FAILURE);
  }
  /*
  * Attempt to connect to the Server
  *
  */
  
  switch (wError = connect(client, result-&gt;ai_addr, result-&gt;ai_addrlen)) {
      case 0:
          cerr &lt;&lt; "Resolved hostname." &lt;&lt; endl;
          break;
      case SOCKET_ERROR:
          wError = WSAGetLastError();
          cerr &lt;&lt; "Error: connet() failed "
                  "Details: " &lt;&lt; wError &lt;&lt; endl;
          closesocket(client);
          freeaddrinfo(result);
          WSACleanup();
          exit(EXIT_FAILURE);
          break;
      default:
          cerr &lt;&lt; "Fatal connect() error: unexpected "
                  "return value." &lt;&lt; endl;
          closesocket(client);
          freeaddrinfo(result);
          WSACleanup();
          exit(EXIT_FAILURE);
          break;
  }
  
  cout &lt;&lt; "Connected to server." &lt;&lt; endl;
  
  closesocket(client);
  freeaddrinfo(result);
  WSACleanup();
  exit(EXIT_SUCCESS);
  }
  </code></pre>