title: 11001 returned on all calls to getaddrinfo()
body: <p>Having an issue connecting to a device on my network. Whenever I call getaddrinfo() it returns 11001. I have checked this with numerous different IP's in the IP_ADDRESS string (Global Var). I've checked all the non-working numbers with nslookup, and most exist there.</p>
  
  <p><a href="https://stackoverflow.com/questions/995532/getaddrinfo-returns-always-11001-host-not-found"> getaddrinfo-returns-always-11001-host-not-found </a>
  seems to be asking a similar question, but there's no answer there.</p>
  
  <p>At the moment, my code is not even trying to connect to the remote device, just trying to resolve an IP. Once that works I can move on to bigger and messier problems.</p>
  
  <p>Implementation:</p>
  
  <pre><code>int connectToDevice(char *sendbuf, char *recvbuf, SOCKET ConnectSocket)
  {
  WSADATA wsaData;
      struct addrinfo *result = NULL,
                  *ptr = NULL,
                  hints;
  struct timeval tval;
  
  fd_set rset, wset;
  
  
  int iResult;
  u_long mode = -1;
  
  //Initialize Winsock
  iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
  if (iResult != 0) 
  {
      printf("WSAStartup failed with error: %d\n", iResult);
      return 1;
  }
  
  ZeroMemory( &amp;hints, sizeof(hints) );
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  
  //Resolve the server address and port
  iResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &amp;hints, &amp;result);
  if ( iResult != 0 ) 
  {
      printf("getaddrinfo failed with error: %d\n", iResult);
      WSACleanup();
      return 1;
  }
  
  
  
  // Attempt to connect to an address until one succeeds
  for(ptr=result; ptr != NULL ;ptr=ptr-&gt;ai_next) 
  {
  
      // Create a SOCKET for connecting to server
      ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);
      if (ConnectSocket == INVALID_SOCKET) 
      {
          printf("socket failed with error: %ld\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
  
      //set socket to non-blocking
      iResult = ioctlsocket(ConnectSocket, FIONBIO, &amp;mode); //if mode is set to non-zero, socket set to non-blocking.
      if(iResult != NO_ERROR)
      {
          printf("socket failed with error: %ld\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
  
  
      // Connect to server.
      iResult = connect( ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR  ) //if an error and not WSAEWOULDBLOCK, then close socket and try next address
      {
          if(WSAEWOULDBLOCK != WSAGetLastError())
          {
              closesocket(ConnectSocket);
              ConnectSocket = INVALID_SOCKET;
              continue;                           //this returns control to the For loop. I.e. if a socket error, try next address
          }
          else    //otherwise if the error was WSAEWOULDBLOCK, then use select to check for connections.
          {
              FD_ZERO(&amp;rset); //initialise fd_sets for reading and writing; both the same.
              FD_SET(ConnectSocket, &amp;rset);
              wset = rset;
  
              //set tval to timeout value
              tval.tv_sec = TIMEOUT;
              tval.tv_usec= 0;
  
              //select statement
              //select ignores first parameter
              //select takes 3xfd_sets, read set, write set, and exception set.
              //select's last parameter is timeout in the form of a timeval struct
              //if return == 0, timeout occured.
              //if return == SOCKET_ERROR, error occured, use WSAGetLastError to check for details.
  
              iResult = select(ConnectSocket, &amp;rset, &amp;wset, NULL, &amp;tval);
              if (iResult ==0)
              {
                  closesocket(ConnectSocket);
                  printf("Timeout reached, closing socket");
                  WSACleanup();
                  return 1;
              }
              else if(iResult == SOCKET_ERROR)
              {
                  printf("socket failed with error: %ld\n", WSAGetLastError());
                  WSACleanup();
                  return 1;
              }
  
          }
  
      }
  
      break;  //Breaks out of the for loop. Will only occur if continue not executed
  }
  
  freeaddrinfo(result);
  
  if (ConnectSocket == INVALID_SOCKET)
  {
      printf("Unable to connect to server!\n");
      WSACleanup();
      return 1;
  }
  
  return 0;}
  </code></pre>
  
  <p>Most of this code has been taken lock and stock from the msdn website, but it all seems to look ok.</p>