#include <winclude.h>
#include <sys/timeb.h>

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
//	time_t ltime;
	struct _timeb timebuffer;

	_ftime( &timebuffer );

	tv->tv_sec=timebuffer.time;
	tv->tv_usec=((timebuffer.time * 1000)+timebuffer.millitm)*100;
	return 0;
};

unsigned int sleep(unsigned int seconds)
{
	Sleep(1000*seconds);
	return(0);
};

void usleep(unsigned long usec)
{
	int actual;

	actual = usec/1000;

	Sleep(actual);
	return;
};

//strcasecmp

int strcasecmp(const char *s1, const char *s2)
{
	int ret;

	char *cp1,*cp2;
	int i=0;

	cp1=malloc(strlen(s1)+1);
	memset(cp1,0,strlen(s1)+1);
	memcpy(cp1,s1,strlen(s1));
    for (i=0; cp1[i]>0; i++)
    {
        if ('a' <= cp1[i] && cp1[i] <= 'z')
            cp1[i] -= 32;
    }

	cp2=malloc(strlen(s2)+1);
	memset(cp2,0,strlen(s2)+1);
	memcpy(cp2,s2,strlen(s2));
    for (i=0; cp2[i]>0; i++)
    {
        if ('a' <= cp2[i] && cp2[i] <= 'z')
            cp2[i] -= 32;
    }


	ret=strcmp(cp1,cp2);
	return ret;
}

int strncasecmp(const char *s1, const char *s2, size_t n)
{
	int ret;
		char *cp1,*cp2;
	int i=0;

	cp1=malloc(strlen(s1)+1);
	memset(cp1,0,strlen(s1)+1);
	memcpy(cp1,s1,strlen(s1));
    for (i=0; cp1[i]>0; i++)
    {
        if ('a' <= cp1[i] && cp1[i] <= 'z')
            cp1[i] -= 32;
    }

	cp2=malloc(strlen(s2)+1);
	memset(cp2,0,strlen(s2)+1);
	memcpy(cp2,s2,strlen(s2));
    for (i=0; cp2[i]>0; i++)
    {
        if ('a' <= cp2[i] && cp2[i] <= 'z')
            cp2[i] -= 32;
    }



	ret=strncmp(cp1,cp2,n);
	return ret;
}

void StartWinsock()
{
	WORD werd;
	WSADATA data;

	werd = MAKEWORD( 1, 1 );
//	printf("Attempting to start winsock... ");
	if( (WSAStartup(werd, &data)) !=0 ){
		printf("failed to start winsock.\n");
		exit(1);
	}
	else{
//		printf("started winsock.\n\n");
	}
}

void StopWinsock()
{
	WSACleanup();
}