#include "Gos.pch"

static HINSTANCE LibWinSock=0;

//
// Winsock functions - for GosView usage only
//

int (WINAPI * _closesocket)( SOCKET s );
int (WINAPI * _recv)( SOCKET s, char *buf, int len, int flags );
int (WINAPI * _recvfrom)( SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR * from, int FAR * fromlen);
int (WINAPI * _send)( SOCKET s, char *buf, int len, int flags );
int (WINAPI * _sendto)( SOCKET s,const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen);
SOCKET (WINAPI * _accept)( SOCKET s, struct sockaddr *addr, int *addrlen );
int (WINAPI * _listen)( SOCKET s, int backlog );
int (WINAPI * _bind)( SOCKET s, const struct sockaddr *name, int namelen );
u_long (WINAPI * _htonl)( u_long hostlong );
u_short (WINAPI * _htons)( u_short hostshort );
SOCKET (WINAPI * _socket)( int af, int type, int protocol );
int (WINAPI * _gethostname)(char FAR * name, int namelen );
struct hostent FAR * (WINAPI * _gethostbyname)(const char FAR * name);
u_long (WINAPI * _ntohl)(u_long netlong);
unsigned long (WINAPI * _inet_addr)(const char FAR * cp);
int (WINAPI * _WSACleanup )(void);
int (WINAPI * _WSAStartup)( WORD wVersionRequested, LPWSADATA lpWSAData );
int (WINAPI * _WSAGetLastError)(void);
struct servent * (WINAPI * _getservbyname)(char *name, char *proto );


//
// Load all libraries GOS depends upon
//
void InitLibraries()
{


//
// Get Winsock
//
	if( 1 ) // Environment.NetworkGame || gEnableGosView )
	{
		LibWinSock = LoadLibrary( _T("ws2_32.dll") );
		if( !LibWinSock )
			LibWinSock = LoadLibrary( _T("wsock32.dll") );
		if( LibWinSock )
		{
			_closesocket = (int (WINAPI *)( SOCKET )) GetProcAddress(LibWinSock, "closesocket");
			_recv = (int (WINAPI *)( SOCKET s, char *buf, int len, int flags )) GetProcAddress(LibWinSock, "recv");
			_recvfrom = (int (WINAPI *) ( SOCKET s, char FAR * buf, int len, int flags, struct sockaddr FAR * from, int FAR * fromlen)) GetProcAddress(LibWinSock, "recvfrom");
			_send = (int (WINAPI *)( SOCKET s, char *buf, int len, int flags )) GetProcAddress(LibWinSock, "send");
			_sendto = (int (WINAPI *)( SOCKET s,const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen)) GetProcAddress(LibWinSock, "sendto");
			_accept = (SOCKET (WINAPI *)( SOCKET s, struct sockaddr *addr, int *addrlen )) GetProcAddress(LibWinSock, "accept");
			_listen = (int (WINAPI *)( SOCKET s, int backlog )) GetProcAddress(LibWinSock, "listen");
			_bind = (int (WINAPI *)( SOCKET s, const struct sockaddr *name, int namelen )) GetProcAddress(LibWinSock, "bind");
			_htonl = (u_long (WINAPI *)( u_long hostlong )) GetProcAddress(LibWinSock, "htonl");
			_htons = (u_short (WINAPI *)( u_short hostshort )) GetProcAddress(LibWinSock, "htons");
			_socket = (SOCKET (WINAPI *)( int af, int type, int protocol )) GetProcAddress(LibWinSock, "socket");
			_gethostname = (int (WINAPI *)(char FAR * name, int namelen )) GetProcAddress(LibWinSock, "gethostname");
			_gethostbyname = (struct hostent FAR * (WINAPI *) (const char FAR * name)) GetProcAddress(LibWinSock, "gethostbyname");
			_ntohl = (u_long (WINAPI *)(u_long netlong)) GetProcAddress(LibWinSock, "ntohl");
			_inet_addr = (unsigned long (WINAPI *)(const char FAR * cp)) GetProcAddress(LibWinSock, "inet_addr");
			_WSACleanup = (int (WINAPI *)( void )) GetProcAddress(LibWinSock, "WSACleanup");
			_WSAStartup = (int (WINAPI *)( WORD wVersionRequested, LPWSADATA lpWSAData )) GetProcAddress(LibWinSock, "WSAStartup");
			_WSAGetLastError = (int (WINAPI *)( void )) GetProcAddress(LibWinSock, "WSAGetLastError");
			_getservbyname = ( struct servent * (WINAPI *) ( char *name, char *proto )) GetProcAddress(LibWinSock, "getservbyname");
		}
	}


}


void DestroyLibraries()
{
	if( LibWinSock )
	{
		FreeLibrary( LibWinSock );
		LibWinSock=0;
	}
}

