title: C++ socket Connect is stuck when trying to connect to a unreachable computer
body: <p>Source Code: </p>
  
  <pre><code>    bool Helper::connectToAComputer(char* destIpString, char* srcIpString)
  {
      int iResult = 0;
      u_long iMode = 1;
      SOCKET ConnectSocket = INVALID_SOCKET;
      ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (ConnectSocket == INVALID_SOCKET) 
      {
          wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
          //return NULL;
      }
  
      sockaddr_in clientService;
      clientService.sin_family = AF_INET;
      clientService.sin_addr.s_addr = inet_addr(destIpString);
      clientService.sin_port = htons(22223);
  
      iResult = connect(ConnectSocket, (SOCKADDR *)&amp; clientService, sizeof(clientService));
      if (iResult == SOCKET_ERROR)
      {
          wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
      }
  
      closesocket(ConnectSocket);
      return false;
  }
  </code></pre>
  
  <p>I am writing a program that will check all local IP addresses to see which computers or devices are on the local network. I have written this function which connects to a socket.   </p>
  
  <p>The problem is that when the socket tries the connect to an IP that is not owned by any device, the program just stops and waits on the connect line. It can connect to IP's that are owned by a computer. I want the function to tell me whether the connect was not successful instead of just hanging and waiting.</p>