/*
 *              Copyright (C) 2005, BroadOn Communications Corp.
 *
 * These coded instructions, statements, and computer programs contain
 * unpublished  proprietary information of BroadOn Communications Corp.,
 * and  are protected by Federal copyright law. They may not be disclosed
 * to  third  parties or copied or duplicated in any form, in whole or in
 * part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>

void 
usage()
{
    printf("Usage: printf [server] [port_number]");
    printf("  Connect vn usb mux program \n");
    printf("\t  -s server ip [default is 127.0.0.1]\n");
    printf("\t  -p port number [default is 20023]\n");
}

int 
main(int argc, char **argv)
{
    char *server = "127.0.0.1", buf[64];
    struct sockaddr_in addr;
    int c, fd, addrLen=sizeof(addr), port=20023, bytes;

#ifdef WIN32
    WSADATA            wsd;
	int rv;
                                                                                                     
    if ((rv = WSAStartup(MAKEWORD(2,2), &wsd)) != 0) {
        printf("WSAStartup() failed: %d\n", rv);
        return -1;
    }
#endif

	if (argc == 3) {
		server = argv[1];
		port = atoi(argv[2]);
	} else if (argc != 1) {
		usage();
		return -1;
	}

    fd = (int) socket(AF_INET, SOCK_STREAM, 0); 
    if (fd <= 0) {
        printf("ERROR: Cannot open AF_INET socket\n");
        return -1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(server);
    addr.sin_port = htons(port);

    if (connect(fd, (struct sockaddr *)&addr, addrLen) < 0) {
        close(fd);
        printf("Cannot connect to NC proxy program\n");
        return -2;
    }
    
    while (1) {
        bytes = recv(fd, buf, sizeof(buf), 0);
        if (bytes > 0) 
            for (c=0; c<bytes; c++) { 
				if (buf[c]) printf("%c", buf[c]);
			    if (buf[c] == '\r' || buf[c] == '\n') 
                    fflush(stdout);
            }
    }

    close(fd);
    return 0;
}
