	#include <stdio.h>
	#include <stdarg.h>
	#include <stdlib.h>
	#include <errno.h>
	#include <string.h>
	#include <winsock.h>
	#include <io.h>
	#include <signal.h>
	#include "our_server.h" 

	char ip_arr[MAX_SERVERS_NUM][20];

	void Recv_Send (int recv_sock, int send_sock)
	{
		int numbytes;
		char buf[MAXDATASIZE];

		if ((numbytes=recv(recv_sock, buf, MAXDATASIZE, 0)) == -1) 
		{
            perror("recv");
			exit(1);
		}
	    
		buf[numbytes] = '\0';

		if ((numbytes= send(send_sock ,buf ,MAXDATASIZE, 0)) == -1)
		{
			perror ("send");
			exit (1);
		}

	
	}

	unsigned long CommunicationThread (int *Sock2Client)
	{
   
	   int  Sock2Server ;
	   int size = sizeof(struct sockaddr);
       struct hostent *he;
       struct sockaddr_in server_addr;/* serve's address information */


	   //struct sockaddr_in Source_Addr;

	   /*printf("enter to accept thread\n");
	   Source_Addr.sin_family        = AF_INET;
	   Source_Addr.sin_port          = htons(SERVERPORT);
	   Source_Addr.sin_addr.s_addr   = INADDR_ANY;*/


		   /*Sock2Client = accept(*ListenSock,(struct sockaddr *)&Source_Addr,&size );
		   if (Sock2Client == -1)
			{
			   perror("accept");
			   continue;
			}
		   printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));*/

		   Sock2Server = socket(AF_INET, SOCK_STREAM, 0); 
		   if (Sock2Server == -1)
			{
				perror("socket");
				exit(1);
			}

			if ((he=gethostbyname(ip_arr[0])) == NULL)  /* get the server ip */
			{
				exit(1);
			}

			server_addr.sin_family = AF_INET;         /* host byte order */
			server_addr.sin_port = htons(SERVERPORT);     /* short, network byte order */
			server_addr.sin_addr = *((struct in_addr *)he->h_addr);
			memset(&(server_addr.sin_zero),0 ,  8);        /* zero the rest of the struct */

			if (connect(Sock2Server, (struct sockaddr *)&server_addr, \
												sizeof(struct sockaddr)) == -1) 
			{
				int err;
				err = WSAGetLastError ();
				perror("connect");
				exit(1);
			}
			
			while (1)
			{
				Recv_Send (*Sock2Client ,Sock2Server);
				Recv_Send (Sock2Server ,*Sock2Client);
			}

	 
		return 0;
}
	
void init_ip_arr(void)
{
	strcpy(ip_arr[0] ,"127.0.0.1");
}

void init_threads_arr(HANDLE *ThredsHandles)
{
	int i;

	for (i=0 ; i<MAX_CLIENTS_NUM ; i++)
	{
		ThredsHandles [i] = NULL;
	}

}


int find_next_free_index(HANDLE *ThredsHandles)
{	
	int i;

	for (i=0 ; i<MAX_CLIENTS_NUM ; i++)
	{
		if (ThredsHandles [i] ==  NULL)
			return i;
	}
	return -1;
}

    int main(int argc, char *argv[])
    {
        int sockfd, Sock2Client;  /* listen on sock_fd, new connection on Sock2Client */
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int sin_size;
        unsigned long temp;
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		int ThreadsIndex = 0;
		HANDLE ThredsHandles[MAX_CLIENTS_NUM];
 
		wVersionRequested = MAKEWORD( 2, 2 );
 
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 ) {
			/* Tell the user that we could not find a usable */
			/* WinSock DLL.                                  */
			return 0;
		}
 
		if ( LOBYTE( wsaData.wVersion ) != 2 ||
				HIBYTE( wsaData.wVersion ) != 2 ) {
			/* Tell the user that we could not find a usable */
			/* WinSock DLL.                                  */
			WSACleanup( );
			return 0; 
		}
 
		/* The WinSock DLL is acceptable. Proceed. */

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
		{
			int err;
			err = WSAGetLastError();
            perror("socket");
            exit(1);
        }

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        memset(&(my_addr.sin_zero),0 ,  8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
		{
            perror("bind");
            exit(1);
        }

        if (listen(sockfd, BACKLOG) == -1) 
		{
            perror("listen");
            exit(1);
        }

		init_ip_arr();
		init_threads_arr(ThredsHandles);

        while(1)  /* main accept() loop */
		{ 
			sin_size = sizeof(struct sockaddr_in);
            if ((Sock2Client = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) 
			{
                perror("accept");
                continue;
            }
            printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
			
			ThreadsIndex = find_next_free_index(ThredsHandles);
			if (ThreadsIndex == -1)
			{
				printf("TOO MANY SESSIONS");
				continue;
			}
					
			ThredsHandles[ThreadsIndex] = CreateThread(NULL,0,(unsigned long (__stdcall *)(void*))CommunicationThread,&Sock2Client,0,&temp);	
 
            
			 
        }
		closesocket(Sock2Client);  /* parent doesn't need this */
			
		WSACleanup ();
    }





