/* tcp_client.c - From Jon C. Snader: Effective TCP/IP Programming
 *           Addison-Wesley 2000
 *           For better understanding, read the excellent Snader book.
 */


#include "etcp.h"

/* tcp_client - set up for a TCP client
                hname: it is either a host name, or a dotted decimal address,
                       or NULL, in which case it is assumed to be INADDR_ANY;
                sname: it is either a port number in host byte order, or
                       the name of a standard service
	   It returns the client socket.
 */
SOCKET tcp_client( char *hname, char *sname )
{
	struct sockaddr_in peer;
	SOCKET s;

	set_address( hname, sname, &peer, "tcp" );
	s = socket( AF_INET, SOCK_STREAM, 0 );
	if ( !isvalidsock( s ) )
		error( 1, errno, "socket call failed" );

	if ( connect( s, ( struct sockaddr * )&peer,
		 sizeof( peer ) ) )
		error( 1, errno, "connect failed" );

	return s;
}

