// Connect to a server at a given port

#ifdef _WIN32
	#include <winsock2.h>
	#pragma comment (lib, "ws2_32.lib")
#else
	#include <socket.h>
#endif // _WIN32

#include <iostream> // Output

main( int argc, char *argv[ ], char *envp[ ] )
{
	if (argc < 2 || argc > 3)
	{
		std::cout << "Usage: " << argv[0] << " Server [ port ] " << std::endl;
		return (0);
	}

#ifdef _WIN32 // Only nessesary under Windows

	WSADATA wsockinfo;

	int retval = WSAStartup(MAKEWORD(1, 1), &wsockinfo);

	if (retval != 0)
	{
		std::cout << "Unable to initialize Winsock! --" << retval << std::endl;
	}
	else
	{
#endif // _WIN32

		// Get information about the server to connect to.
		hostent *pHostinfo = gethostbyname(argv[1]);

		if (pHostinfo == NULL)
		{
			std::cout << "Unable to resolve host " << argv[1] << std::endl;
		}
		else
		{
   	   // Copy information about the server into the structure.
      	if (pHostinfo->h_addrtype != AF_INET)
         {
            std::cout << "Couldn't get IP address of " << argv[1] << std::endl;
         }
         else
         {
	         // Create a socket.
	         SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);

	         if (sock == SOCKET_ERROR)
            {
               std::cout << "Unable to create socket!" << std::endl;
            }
            else
            {
            	// Make a connection to server
               sockaddr_in sockinfo;
               memset(&sockinfo, 0, sizeof(sockinfo));

         		sockinfo.sin_family = AF_INET; // Internet

               int port = 7;  // echo
               if (argc == 3) // A special port was given
               {
                  port = atoi(argv[2]);
                  
                  if (port == 0)
                  {
                     servent *pEnt = getservbyname(argv[2], "tcp");
                     if (pEnt)
                        port = ntohs(pEnt->s_port);
                  }
               }

		         sockinfo.sin_port = htons(port);

		         // Connect to this IP address.
		         memcpy(&sockinfo.sin_addr, pHostinfo->h_addr, 4);

               std::cout << "Attempting to connect to " 
                         << int(sockinfo.sin_addr.S_un.S_un_b.s_b1) << "." 
                         << int(sockinfo.sin_addr.S_un.S_un_b.s_b2) << "." 
                         << int(sockinfo.sin_addr.S_un.S_un_b.s_b3) << "." 
                         << int(sockinfo.sin_addr.S_un.S_un_b.s_b4) 
                         << " on port " << port << "  ...." ;

	            retval = connect(sock, (sockaddr *) &sockinfo, sizeof(sockinfo));

	            if (retval != 0)
               {
                  std::cout << "Unable to connect!" << std::endl;
               }
               else
               {
                  std::cout << "connected" << std::endl;
               }

               closesocket(sock);
            }
         }

      }

#ifdef _WIN32
		WSACleanup();
	}
#endif // _WIN32

	return 0;
}


