/* CHANGES FROM UNIX VERSION                                                   */
/*                                                                             */
/* 1.  Changed header files.                                                   */
/* 2.  Added WSAStartUP() and WSACleanUp().                                    */
/* 3.  Used closesocket() instead of close().                                  */ 

#include <stdio.h>      /* for printf(), fprintf() */
#include <winsock.h>    /* for socket(),... */
#include <stdlib.h>     /* for exit() */
#include "blimpcli.h"
#include "winamp.h"

#define RCVBUFSIZE 32   /* Size of receive buffer */



HWND get_winamp_handle( void )
{
    HWND hwnd_winamp = FindWindow("Winamp v1.x",NULL);
    return hwnd_winamp;
}



int start_client( )
{
    int sock;                        /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    unsigned short echoServPort;     /* Echo server port */
    char *servIP;                    /* Server IP address (dotted quad) */
    char *echoString;                /* String to send to echo server */
    char echoBuffer[RCVBUFSIZE];     /* Buffer for echo string */
    int echoStringLen;               /* Length of string to echo */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() and total bytes read */
    WSADATA wsaData;                 /* Structure for WinSock setup communication */
    
    echoString = "running";
    servIP = "10.5.0.165";
    echoServPort = 32379;
    echoStringLen = strlen(echoString);


    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) /* Load Winsock 2.0 DLL */
    {
        fprintf(stderr, "WSAStartup() failed");
        exit(1);
    }
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        printf("socket() failed");
   

    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);   /* Server IP address */
    echoServAddr.sin_port        = htons(echoServPort); /* Server port */
    

    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        printf("connect() failed");

    if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
	    printf("send() sent a different number of bytes than expected");
    
    HWND hWinAmp = get_winamp_handle( );
    //error checking...

    while( strcmp(echoBuffer,"quit") != 0 ){

	echoStringLen = strlen(echoString);          
	totalBytesRcvd = 0;
		
	while (totalBytesRcvd < 10)
	{
       
	    if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
		printf("recv() failed or connection closed prematurely");
	    totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */
	    echoBuffer[bytesRcvd] = '\0';  /* Add \0 so printf knows where to stop */
	}
	
	if( strcmp(echoBuffer,"IPC_STARTPLAY") == 0 ){
	    printf("I have been told to start playing winamp\n");
	    SendMessage(hWinAmp,WM_WA_IPC,0,IPC_STARTPLAY);
	}
	else{
	    printf("setting winamp to position: %s\n",echoBuffer);
	    SendMessage(hWinAmp,WM_WA_IPC,atoi(echoBuffer)+100,IPC_JUMPTOTIME);
	}

	
    }//while

  
    closesocket(sock);
    WSACleanup();  /* Cleanup Winsock */
    exit(0);
}



