///////////////////////////////////////////////////////////////
//! 05-separateTCP
//!-------------
//! configClient connects to configServer 
//! done: The server sends initial configuration data (calibre, 
//!			color and exit assignments) to the configClient
//!	done: After the configServer sends the configuration data, the
//!			server sends 'X'. 
//!	done: After this point, the configClient enters an infinite loop
//!			to read any configuration updates from the configServer
//!			The PDI program itself sends processing data to the opDataServer
//!	todo: The configClient shares memory with the image processing
//!			routines, so the configuration data is shared. 
//!			Put semaphores on the memory, to prevent corruption
//------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <netinet/in.h>
#include <string.h>

#include "tcpClient.h"

#define MAXBUF          1024

///////////////////////////////////////////////////////////////
//! The initial buffer has the via and ip of this configClient
//! Open socket for streaming---
///////////////////////////////////////////////////////////////
int tcpClient(char *ip, uint16_t port) { 
	int sockfd;
    struct sockaddr_in dest;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
        perror("socket()");
		return(-1);
    	}

    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(port);
    if (inet_aton(ip, &dest.sin_addr) == 0) {
        perror(ip);
		return(-1);
    	}

    //---Connect to server---
    if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) < 0) {
		perror("connect()");
		return(-1);
		}

	return(sockfd);
	}


