title: Cannot connect to socket
body: <p>Hey im writing an echo client, and for some reason the connectsock function is returning an error, and returning an INVALID_SOCKET. I can't figure out why is this. Can someone tell me why</p>
  
  <pre><code>/*********************************
  *   echo1.cpp                    *
  *                                *
  *   Echo client - version 1      *
  **********************************/
  #include &lt;winsock2.h&gt;
  #pragma comment(lib, "ws2_32.lib")
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  const int  MAXBUF  = 128;
  const int  MAXNAME = 80;
  
  SOCKET connectsock (char*, char*, char*);
  
  int main (int argc, char **argv) 
  {
      char msg[MAXBUF] = "";
      char buf[MAXBUF] = "";
      char host[MAXNAME] = "";
      char service[MAXNAME] = "";
      int  len;
      SOCKET s;
  
      /* Must always initialize the Windows Sockets TCP/IP library */
      WORD wVersion = 0x0202;
      WSADATA wsaData;
      int iResult = WSAStartup( wVersion, &amp;wsaData);
      if (iResult != 0) 
      {
          cout &lt;&lt; "Unable to initialize Windows Socket library." &lt;&lt; endl;
          return 0;
      }
  
  
      string hostaddress = argv[1];
  
      if(argv[2] == NULL)
      {
          strcpy_s(service, MAXNAME, "7");
      }
      else
      {
          strcpy_s(service, MAXNAME, argv[2]);
      }
  
      strcpy_s( host, MAXNAME, hostaddress.c_str() );
  
  
  
      s = connectsock(host, service, "tcp");
      if (s != INVALID_SOCKET) 
      {
  
          cout &lt;&lt;"Message?";
          cin.getline(msg, MAXBUF);
  
  
          // Now, let's try to send the message to the remote machine
  
  
          len = send(s, msg, (int) strlen(msg), 0);
          if (len &gt; 0) 
          {
              cout &lt;&lt; "Number of bytes sent: " &lt;&lt; len &lt;&lt; endl;
              cout &lt;&lt; endl;
          } 
          else 
          {
              cout &lt;&lt; "Unable to send message to remote machine." &lt;&lt; endl;
              return 0;
          }
  
          // Message was sent.  Is there a reply from the remote machine?
          len = recv(s, buf, sizeof(buf), 0);
          if (len &gt; 0) 
          {
              cout &lt;&lt; "Received message: ";
              for (int i=0; i&lt;len; i++) cout &lt;&lt; buf[i];
              cout &lt;&lt; endl;
              cout &lt;&lt; "Number of bytes received: " &lt;&lt; len &lt;&lt; endl &lt;&lt; endl;
          }
          cout &lt;&lt; endl &lt;&lt; endl;
  
          /* Close the socket (and thus the connection) */
          shutdown(s,1);
          closesocket(s);
      }
      WSACleanup();
  }
  
  
  /*------------------------------------------------------------------
  * connectsock - allocate &amp; connect a remote socket using TCP or UDP
  *------------------------------------------------------------------
  */
  SOCKET connectsock (char *host, char *service, char *protocol)
  {   
      struct  hostent *phe;   
      struct  servent *pse;
      short   port;   
      int     ihost;  
  
  
      port = htons( (u_short) atoi(service)); // 1st try to convert string to integer
      if (port == 0)
      {                       // if that doesn't work, call service function
          pse = getservbyname(service,NULL);
          if (pse) 
          {
              port = pse-&gt;s_port;
          }
          else
          {
              cout &lt;&lt; "Invalid service request." &lt;&lt; endl;
              return INVALID_SOCKET;
          }
      }
  /*
      cout &lt;&lt; "Service: " &lt;&lt; service &lt;&lt; endl;
      cout &lt;&lt; "Port:    " &lt;&lt; htons(port) &lt;&lt; endl;
  */
  
  
      ihost = inet_addr(host);    
      if (ihost == INADDR_NONE) 
      {   
          phe = gethostbyname(host);
          if (phe)
          {
              memmove(&amp;ihost, phe-&gt;h_addr, phe-&gt;h_length);
          }
          else
          {
              cout &lt;&lt; "Invalid Host." &lt;&lt; endl;
              return INVALID_SOCKET;
          }
      }
  /*
      cout &lt;&lt; "Network address for hostname:         " &lt;&lt; host &lt;&lt; endl;
      cout &lt;&lt; "32-bit address in Network Byte Order: " &lt;&lt; ihost &lt;&lt; endl;
      cout &lt;&lt; "32-bit address in Host Byte Order:    " &lt;&lt; ntohl(ihost) &lt;&lt; endl;
  
      struct in_addr in;
      in.S_un.S_addr = ihost;
      cout &lt;&lt; "Address as dotted decimal:            " &lt;&lt; inet_ntoa(in) &lt;&lt; endl;
  
  */
  
      struct sockaddr_in remote;  
      SOCKET s;                   
  
      if (_stricmp(protocol, "tcp") == 0) 
      {   
          s = socket(AF_INET, SOCK_STREAM, 0);
  
          if (s &lt; 0 || s == INVALID_SOCKET)
          {
              cout &lt;&lt; "Cannot create socket" &lt;&lt; endl;
              return INVALID_SOCKET;
          }
  
  
          memset(&amp;remote, 0, sizeof(remote));
          remote.sin_family = AF_INET;
          remote.sin_port = htons( (u_short) atoi(service));
          remote.sin_addr.s_addr = inet_addr(host);
  
  
          int status = connect(s, (LPSOCKADDR) &amp;remote, sizeof(SOCKADDR));
  
          if (status == SOCKET_ERROR) 
          {
              cout &lt;&lt; "Remote host/service not found - or connection refused" &lt;&lt; endl;
              return INVALID_SOCKET;
          }
      }
  
      else if (_stricmp(protocol,"udp") == 0) 
      {   
          s = socket(AF_INET, SOCK_DGRAM,  0);
          if (s &lt; 0 || s == INVALID_SOCKET) 
          {
              cout &lt;&lt; "Cannot create socket" &lt;&lt; endl;
              return INVALID_SOCKET;
          }
      }
      else 
      {                                   // Unknown or unsupported protocol request
          cout &lt;&lt; "Invalid Protocol" &lt;&lt; endl;
          return INVALID_SOCKET;
      }
  
      return s;
  }
  </code></pre>