/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		This class represents the base networking system on the machine the game is being run on.
///				A call to Initialize() must be made before creating any networked objects, and no network
///				objects must be made unless initialize() was successful.
//=====================================================================================================================

#include "Precompiled.h"
#include "Network\NetworkCore.h"

using namespace Game;

//=====================================================================================================================
// Statics
//=====================================================================================================================

/// True if the network system has been initialized successfully

bool NetworkCore::s_initialized = false;

/// A map from winsock error codes to error descriptions

std::map<int,const char *> * NetworkCore::s_winsock_errors = 0;

//=====================================================================================================================
/// @brief		Initializes the network subsystem
/// @pre		The network system must not be initialized already
//=====================================================================================================================

void NetworkCore::Initialize()
{
	// Make sure ok:

	ENSURE( IsInitialized() == false , "The network system must not be initialized already" )
	
	// This is the version of winsock we want 2.2:
	
	WORD winsock_version = MAKEWORD( 2 , 2 );
	
	// Store the version info received here:
	
	WSAData wsa_startup_info;
	
	// Do the startup
	
	int result = WSAStartup( winsock_version , &wsa_startup_info );
	
	// See if that was ok:
	
	if ( result == 0 )
	{
		// Grand: set the initialized flag to true
		
		s_initialized = true;
		
		// Make the errors map:
		
		s_winsock_errors = new std::map<int,const char *>();
		
		// Build the errors list
		
		BuildErrorsList();
	}
}

//=====================================================================================================================
/// @brief		Shuts down the network subsystem
/// @pre		The network system must already be initialized
//=====================================================================================================================

void NetworkCore::Shutdown()
{
	// Make sure ok:

	ENSURE( IsInitialized() , "The network system must already be initialized" )
	
	// Shutdown:
	
	WSACleanup();
	
	// Not initialized any more
	
	s_initialized = false;
	
	// Remove the winsock errors map:
	
	delete s_winsock_errors; s_winsock_errors = 0;
}

//=====================================================================================================================
/// @brief		Clears the current error code
/// @pre		The network system must already be initialized
//=====================================================================================================================

void NetworkCore::ClearError()
{
	// Make sure ok:

	ENSURE( IsInitialized() , "The network system must already be initialized" )
	
	// Clear the error:
	
	WSASetLastError(0);
}

//=====================================================================================================================
/// @brief		Gets the current error code
/// @pre		The network system must already be initialized
//=====================================================================================================================

int NetworkCore::GetErrorCode()
{
	// Make sure ok:

	ENSURE( IsInitialized() , "The network system must already be initialized" )
	
	// Return the error code:
	
	return WSAGetLastError();
}

//=====================================================================================================================
/// @brief		Gets the current error string for the current error code
/// @pre		The network system must already be initialized
/// @pre		The errors map must have been created
//=====================================================================================================================

const char * NetworkCore::GetErrorString()
{
	// Make sure ok:

	ENSURE( IsInitialized()		, "The network system must already be initialized"	)
	ENSURE( s_winsock_errors	, "The errors map must have been created"			)
	
	// Get the last error code:
	
	int error_code = GetErrorCode();
	
	// Look in the errors map:
	
	std::map<int,const char *>::iterator i = s_winsock_errors->find(error_code);
	
	// See if found:
	
	if ( i != s_winsock_errors->end() )
	{
		// Return the error:
		
		return i->second;
	}
	else
	{
		// Return an unknown error
		
		return "WSA_UNKNOWN_ERROR\n. Winsock has thrown some kind of unknown error.";
	}
}

//=====================================================================================================================
/// @brief		Builds the list of winsock errors and sticks them into the errors map
/// @pre		The winsock error map should have been created and must be empty
//=====================================================================================================================

void NetworkCore::BuildErrorsList()
{
	// Make sure ok:
	
	ENSURE( s_winsock_errors && s_winsock_errors->size() == 0 , "The winsock error map should have been created and must be empty" )
	
	// Build the error list:
	
	s_winsock_errors->operator[](0) = "NO_ERROR\n.No winsock error has occured.";
	
	// All the rest of the errors:

	s_winsock_errors->operator[](WSA_INVALID_HANDLE) = 
	"WSA_INVALID_HANDLE\n"
	"Specified event object handle is invalid.\n"
	"An application attempts to use an event object, but the specified handle is not valid. Note that this error is returned by the operating system,\n"
	"so the error number may change in future releases of Windows.\n";

	s_winsock_errors->operator[](WSA_NOT_ENOUGH_MEMORY) =  
	"WSA_NOT_ENOUGH_MEMORY\n"
	"Insufficient memory available.\n"
	"An application used a Windows Sockets function that directly maps to a Windows function. The Windows function is indicating\n"
	"a lack of required memory resources. Note that this error is returned by the operating system, so the error number \n"
	"may change in future releases of Windows.\n";
	 
	s_winsock_errors->operator[](WSA_INVALID_PARAMETER) = 
	"WSA_INVALID_PARAMETER\n"
	"One or more parameters are invalid. \n"
	"An application used a Windows Sockets function which directly maps to a Windows function. The Windows function is indicating a \n"
	"problem with one or more parameters. Note that this error is returned by the operating system, so the error number \n";
	"may change in future releases of Windows.\n";
	 
	s_winsock_errors->operator[](WSA_OPERATION_ABORTED) = 
	"WSA_OPERATION_ABORTED\n"
	"Overlapped operation aborted. \n"
	"An overlapped operation was canceled due to the closure of the socket, or the execution of the SIO_FLUSH command in WSAIoctl. \n"
	"Note that this error is returned by the operating system, so the error number may change in future releases of Windows.\n";

	s_winsock_errors->operator[](WSA_IO_INCOMPLETE) = 
	"WSA_IO_INCOMPLETE\n"
	"Overlapped I/O event object not in signaled state. \n"
	"The application has tried to determine the status of an overlapped operation which is not yet completed. Applications that \n"
	"use WSAGetOverlappedResult (with the fWait flag set to FALSE) in a polling mode to determine when an overlapped operation has \n"
	"completed, get this error code until the operation is complete. Note that this error is returned by the operating system, so \n"
	"the error number may change in future releases of Windows.\n";
	 
	s_winsock_errors->operator[](WSA_IO_PENDING) = 
	"WSA_IO_PENDING\n"
	"Overlapped operations will complete later. \n"
	"The application has initiated an overlapped operation that cannot be completed immediately. A completion indication \n"
	"will be given later when the operation has been completed. Note that this error is returned by the operating system, \n"
	"so the error number may change in future releases of Windows.\n";

	s_winsock_errors->operator[](WSAEINTR) = 
	"WSAEINTR\n"
	"Interrupted function call. \n"
	"A blocking operation was interrupted by a call to WSACancelBlockingCall.\n";
	 
	s_winsock_errors->operator[](WSAEBADF) = 
	"WSAEBADF\n"
	"File handle is not valid. \n"
	"The file handle supplied is not valid.\n";

	s_winsock_errors->operator[](WSAEACCES) = 
	"WSAEACCES\n"
	"Permission denied. \n"
	"An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto without \n"
	"broadcast permission being set using setsockopt(SO_BROADCAST). \n"
	"Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4 SP4 or later), another application, \n"
	"service, or kernel mode driver is bound to the same address with exclusive access. Such exclusive access is a new feature of Windows NT 4 SP4 \n"
	"and later, and is implemented by using the SO_EXCLUSIVEADDRUSE option.\n";

	s_winsock_errors->operator[](WSAEFAULT) = 
	"WSAEFAULT\n"
	"Bad address.\n" 
	"The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an \n"
	"application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an \n"
	"argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).\n";

	s_winsock_errors->operator[](WSAEINVAL) = 
	"WSAEINVAL\n"
	"Invalid argument. \n"
	"Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function). In some instances, \n"
	"it also refers to the current state of the socket-for instance, calling accept on a socket that is not listening.\n";
	 
	s_winsock_errors->operator[](WSAEMFILE) = 
	"WSAEMFILE\n"
	"Too many open files. \n"
	"Too many open sockets. Each implementation may have a maximum number of socket handles available, either globally, per process, or per thread.\n";
	 
	s_winsock_errors->operator[](WSAEWOULDBLOCK) = 
	"WSAEWOULDBLOCK\n"
	"Resource temporarily unavailable. \n"
	"This error is returned from operations on nonblocking sockets that cannot be completed immediately, for example recv when no \n"
	"data is queued to be read from the socket. It is a nonfatal error, and the operation should be retried later. It is normal for \n"
	"WSAEWOULDBLOCK to be reported as the result from calling connect on a nonblocking SOCK_STREAM socket, since some time \n"
	"must elapse for the connection to be established.\n";
	 
	s_winsock_errors->operator[](WSAEINPROGRESS) = 
	"WSAEINPROGRESS\n"
	"Operation now in progress. \n"
	"A blocking operation is currently executing. Windows Sockets only allows a single blocking operation-per- task or thread-to be \n"
	"outstanding, and if any other function call is made (whether or not it references that or any other socket) the function fails with the WSAEINPROGRESS error.\n";
	 
	s_winsock_errors->operator[](WSAEALREADY) = 
	"WSAEALREADY\n"
	"Operation already in progress. \n"
	"An operation was attempted on a nonblocking socket with an operation already in progress-that is, calling connect a second \n"
	"time on a nonblocking socket that is already connecting, or canceling an asynchronous request (WSAAsyncGetXbyY) that has already been canceled or completed.\n";
	 
	s_winsock_errors->operator[](WSAENOTSOCK) = 
	"WSAENOTSOCK\n"
	"Socket operation on nonsocket. \n"
	"An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a \n"
	"valid socket, or for select, a member of an fd_set was not valid.\n";
	 
	s_winsock_errors->operator[](WSAEDESTADDRREQ) = 
	"WSAEDESTADDRREQ\n"
	"Destination address required. \n"
	"A required address was omitted from an operation on a socket. For example, this error is returned if sendto is called \n"
	"with the remote address of ADDR_ANY.\n";
	 
	s_winsock_errors->operator[](WSAEMSGSIZE) = 
	"WSAEMSGSIZE\n"
	"Message too long. \n"
	"A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the \n"
	"buffer used to receive a datagram was smaller than the datagram itself.\n";
	 
	s_winsock_errors->operator[](WSAEPROTOTYPE) = 
	"WSAEPROTOTYPE\n"
	"Protocol wrong type for socket. \n"
	"A protocol was specified in the socket function call that does not support the semantics of the socket type requested.\n"
	"For example, the ARPA Internet UDP protocol cannot be specified with a socket type of SOCK_STREAM.\n";
	 
	s_winsock_errors->operator[](WSAENOPROTOOPT) = 
	"WSAENOPROTOOPT\n"
	"Bad protocol option. \n"
	"An unknown, invalid or unsupported option or level was specified in a getsockopt or setsockopt call.\n";
	 
	s_winsock_errors->operator[](WSAEPROTONOSUPPORT) = 
	"WSAEPROTONOSUPPORT\n"
	"Protocol not supported. \n"
	"The requested protocol has not been configured into the system, or no implementation for it exists. For example, a socket \n"
	"call requests a SOCK_DGRAM socket, but specifies a stream protocol.\n";
	 
	s_winsock_errors->operator[](WSAESOCKTNOSUPPORT) = 
	"WSAESOCKTNOSUPPORT\n"
	"Socket type not supported. \n"
	"The support for the specified socket type does not exist in this address family. For example, the optional type SOCK_RAW \n"
	"might be selected in a socket call, and the implementation does not support SOCK_RAW sockets at all.\n";
	 
	s_winsock_errors->operator[](WSAEOPNOTSUPP) = 
	"WSAEOPNOTSUPP\n"
	"Operation not supported. \n"
	"The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor \n"
	"to a socket that cannot support this operation is trying to accept a connection on a datagram socket.\n";
	 
	s_winsock_errors->operator[](WSAEPFNOSUPPORT) = 
	"WSAEPFNOSUPPORT\n"
	"Protocol family not supported. \n"
	"The protocol family has not been configured into the system or no implementation for it exists. This message has a \n"
	"slightly different meaning from WSAEAFNOSUPPORT. However, it is interchangeable in most cases, and all Windows Sockets\n" 
	"functions that return one of these messages also specify WSAEAFNOSUPPORT.\n";
	 
	s_winsock_errors->operator[](WSAEAFNOSUPPORT) = 
	"WSAEAFNOSUPPORT\n"
	"Address family not supported by protocol family. \n"
	"An address incompatible with the requested protocol was used. All sockets are created with an associated address \n"
	"family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM). This error is \n"
	"returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family \n"
	"is used for a socket, for example, in sendto.\n";
	 
	s_winsock_errors->operator[](WSAEADDRINUSE) = 
	"WSAEADDRINUSE\n"
	"Address already in use. \n"
	"Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if \n"
	"an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, \n"
	"or a socket that was not closed properly, or one that is still in the process of closing. For server applications \n"
	"that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). \n"
	"Client applications usually need not call bind at all- connect chooses an unused port automatically. When bind \n"
	"is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the \n"
	"specific address is committed. This could happen with a call to another function later, including connect, listen, \n"
	"WSAConnect, or WSAJoinLeaf.\n";
	 
	s_winsock_errors->operator[](WSAEADDRNOTAVAIL) = 
	"WSAEADDRNOTAVAIL\n"
	"Cannot assign requested address. \n"
	"The requested address is not valid in its context. This normally results from an attempt to bind to an address \n"
	"that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or \n"
	"WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).\n";
	 
	s_winsock_errors->operator[](WSAENETDOWN) = 
	"WSAENETDOWN\n"
	"Network is down. \n"
	"A socket operation encountered a dead network. This could indicate a serious failure of the network system \n"
	"(that is, the protocol stack that the Windows Sockets DLL runs over), the network interface, or the local network itself.\n";
	 
	s_winsock_errors->operator[](WSAENETUNREACH) = 
	"WSAENETUNREACH\n"
	"Network is unreachable. \n"
	"A socket operation was attempted to an unreachable network. This usually means the local software knows no \n"
	"route to reach the remote host.\n";
	 
	s_winsock_errors->operator[](WSAENETRESET) = 
	"WSAENETRESET\n"
	"Network dropped connection on reset. \n"
	"The connection has been broken due to keep-alive activity detecting a failure while the operation was \n"
	"in progress. It can also be returned by setsockopt if an attempt is made to set SO_KEEPALIVE on a connection that has already failed.\n";
	 
	s_winsock_errors->operator[](WSAECONNABORTED) = 
	"WSAECONNABORTED\n"
	"Software caused connection abort. \n"
	"An established connection was aborted by the software in your host computer, possibly due to a data \n"
	"transmission time-out or protocol error.\n";
	 
	s_winsock_errors->operator[](WSAECONNRESET) = 
	"WSAECONNRESET\n"
	"Connection reset by peer. \n"
	"An existing connection was forcibly closed by the remote host. This normally results if the peer application on \n"
	"the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or \n"
	"the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). \n"
	"This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more \n"
	"operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.\n";
	 
	s_winsock_errors->operator[](WSAENOBUFS) = 
	"WSAENOBUFS\n"
	"No buffer space available. \n"
	"An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.\n";
	 
	s_winsock_errors->operator[](WSAEISCONN) = 
	"WSAEISCONN \n"
	"Socket is already connected. \n"
	"A connect request was made on an already-connected socket. Some implementations also return this error if \n"
	"sendto is called on a connected SOCK_DGRAM socket (for SOCK_STREAM sockets, the to parameter in sendto is ignored) \n"
	"although other implementations treat this as a legal occurrence.\n";
	 
	s_winsock_errors->operator[](WSAENOTCONN) = 
	"WSAENOTCONN\n"
	"Socket is not connected. \n"
	"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) \n"
	"no address was supplied. Any other type of operation might also return this error-for example, setsockopt setting SO_KEEPALIVE \n"
	"if the connection has been reset.\n";
	 
	s_winsock_errors->operator[](WSAESHUTDOWN) = 
	"WSAESHUTDOWN\n"
	"Cannot send after socket shutdown. \n"
	"A request to send or receive data was disallowed because the socket had already been shut down in that direction with a \n"
	"previous shutdown call. By calling shutdown a partial close of a socket is requested, which is a signal that \n"
	"sending or receiving, or both have been discontinued.\n";
	 
	s_winsock_errors->operator[](WSAETOOMANYREFS) = 
	"WSAETOOMANYREFS\n"
	"Too many references. \n"
	"Too many references to some kernel object.\n";
	 
	s_winsock_errors->operator[](WSAETIMEDOUT) = 
	"WSAETIMEDOUT\n"
	"Connection timed out. \n"
	"A connection attempt failed because the connected party did not properly respond after a period \n"
	"of time, or the established connection failed because the connected host has failed to respond.\n";
	 
	s_winsock_errors->operator[](WSAECONNREFUSED) = 
	"WSAECONNREFUSED\n"
	"Connection refused. \n"
	"No connection could be made because the target computer actively refused it. This usually results \n"
	"from trying to connect to a service that is inactive on the foreign host-that is, one with no server application running.\n";
	 
	s_winsock_errors->operator[](WSAELOOP) = 
	"WSAELOOP\n"
	"Cannot translate name. \n"
	"Cannot translate a name.\n";
	 
	s_winsock_errors->operator[](WSAENAMETOOLONG) = 
	"WSAENAMETOOLONG\n"
	"Name too long. \n"
	"A name component or a name was too long.\n";
	 
	s_winsock_errors->operator[](WSAEHOSTDOWN) = 
	"WSAEHOSTDOWN\n"
	"Host is down. \n"
	"A socket operation failed because the destination host is down. A socket operation encountered a dead host. \n"
	"Networking activity on the local host has not been initiated. These conditions are more likely to be \n"
	"indicated by the error WSAETIMEDOUT.\n";
	 
	s_winsock_errors->operator[](WSAEHOSTUNREACH) = 
	"WSAEHOSTUNREACH\n"
	"No route to host. \n"
	"A socket operation was attempted to an unreachable host. See WSAENETUNREACH.\n";
	 
	s_winsock_errors->operator[](WSAENOTEMPTY) = 
	"WSAENOTEMPTY\n"
	"Directory not empty. \n"
	"Cannot remove a directory that is not empty.\n";
	 
	s_winsock_errors->operator[](WSAEPROCLIM) = 
	"WSAEPROCLIM\n"
	"Too many processes. \n"
	"A Windows Sockets implementation may have a limit on the number of applications that can use it simultaneously. \n"
	"WSAStartup may fail with this error if the limit has been reached.\n";
	 
	s_winsock_errors->operator[](WSAEUSERS) = 
	"WSAEUSERS\n"
	"User quota exceeded. \n"
	"Ran out of user quota.\n";
	 
	s_winsock_errors->operator[](WSAEDQUOT) = 
	"WSAEDQUOT\n"
	"Disk quota exceeded. \n"
	"Ran out of disk quota.\n";
	 
	s_winsock_errors->operator[](WSAESTALE) = 
	"WSAESTALE\n"
	"Stale file handle reference. \n"
	"The file handle reference is no longer available.\n";
	 
	s_winsock_errors->operator[](WSAEREMOTE) = 
	"WSAEREMOTE\n"
	"Item is remote. \n"
	"The item is not available locally.\n";
	 
	s_winsock_errors->operator[](WSASYSNOTREADY) = 
	"WSASYSNOTREADY\n"
	"Network subsystem is unavailable. \n"
	"This error is returned by WSAStartup if the Windows Sockets implementation cannot function at this time because the underlying system it \n"
	"uses to provide network services is currently unavailable. Users should check:\n"
	"That the appropriate Windows Sockets DLL file is in the current path. \n"
	"That they are not trying to use more than one Windows Sockets implementation simultaneously. If there is more than one Winsock \n"
	"DLL on your system, be sure the first one in the path is appropriate for the network subsystem currently loaded. \n"
	"The Windows Sockets implementation documentation to be sure all necessary components are currently installed and configured correctly. \n";
	 
	s_winsock_errors->operator[](WSAVERNOTSUPPORTED) = 
	"WSAVERNOTSUPPORTED \n"
	"Winsock.dll version out of range. \n"
	"The current Windows Sockets implementation does not support the Windows Sockets specification version requested by the \n"
	"application. Check that no old Windows Sockets DLL files are being accessed.\n";
	 
	s_winsock_errors->operator[](WSANOTINITIALISED) = 
	"WSANOTINITIALISED\n"
	"Successful s_winsock_errors->operator[]( ) = WSAStartup not yet performed. \n"
	"Either the application has not called WSAStartup or s_winsock_errors->operator[]( ) = WSAStartup failed. The application \n"
	"may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or \n"
	"WSACleanup has been called too many times.\n";
	 
	s_winsock_errors->operator[](WSAEDISCON) = 
	"WSAEDISCON\n"
	"Graceful shutdown in progress. \n"
	"Returned by WSARecv and WSARecvFrom to indicate that the remote party has initiated a graceful shutdown sequence.\n";
	 
	s_winsock_errors->operator[](WSAENOMORE) = 
	"WSAENOMORE\n"
	"No more results. \n"
	"No more results can be returned by the WSALookupServiceNext function.\n";
	 
	s_winsock_errors->operator[](WSAECANCELLED) = 
	"WSAECANCELLED\n"
	"Call has been canceled. \n"
	"A call to the WSALookupServiceEnd function was made while this call was still processing. The call has been canceled.\n";
	 
	s_winsock_errors->operator[](WSAEINVALIDPROCTABLE) = 
	"WSAEINVALIDPROCTABLE\n"
	"Procedure call table is invalid. \n"
	"The service provider procedure call table is invalid. A service provider returned a bogus procedure table to Ws2_32.dll. \n"
	"This is usually caused by one or more of the function pointers being NULL.\n";
	 
	s_winsock_errors->operator[](WSAEINVALIDPROVIDER) = 
	"WSAEINVALIDPROVIDER\n"
	"Service provider is invalid. \n"
	"The requested service provider is invalid. This error is returned by the WSCGetProviderInfo and WSCGetProviderInfo32 \n"
	"functions if the protocol entry specified could not be found. This error is also returned if the service provider \n"
	"returned a version number other than 2.0.\n";
	 
	s_winsock_errors->operator[](WSAEPROVIDERFAILEDINIT) = 
	"WSAEPROVIDERFAILEDINIT\n"
	"Service provider failed to initialize. \n"
	"The requested service provider could not be loaded or initialized. This error is returned if either a service \n"
	"provider's DLL could not be loaded (LoadLibrary failed) or the provider's WSPStartup or NSPStartup function failed.\n";
	 
	s_winsock_errors->operator[](WSASYSCALLFAILURE) = 
	"WSASYSCALLFAILURE\n"
	"System call failure. \n"
	"A system call that should never fail has failed. This is a generic error code, returned under various conditions.\n"
	"Returned when a system call that should never fail does fail. For example, if a call to WaitForMultipleEvents \n"
	"fails or one of the registry functions fails trying to manipulate the protocol/namespace catalogs.\n"
	"Returned when a provider does not return SUCCESS and does not provide an extended error code. Can indicate a \n"
	"service provider implementation error.\n";
	 
	s_winsock_errors->operator[](WSASERVICE_NOT_FOUND) = 
	"WSASERVICE_NOT_FOUND\n"
	"Service not found. \n"
	"No such service is known. The service cannot be found in the specified name space.\n";
	 
	s_winsock_errors->operator[](WSATYPE_NOT_FOUND) = 
	"WSATYPE_NOT_FOUND\n"
	"Class type not found. \n"
	"The specified class was not found.\n";
	 
	s_winsock_errors->operator[](WSA_E_NO_MORE) = 
	"WSA_E_NO_MORE \n"
	"No more results. \n"
	"No more results can be returned by the WSALookupServiceNext function.\n";
	 
	s_winsock_errors->operator[](WSA_E_CANCELLED) = 
	"WSA_E_CANCELLED\n"
	"Call was canceled. \n"
	"A call to the WSALookupServiceEnd function was made while this call was still processing. The call has been canceled.\n";
	 
	s_winsock_errors->operator[](WSAEREFUSED) = 
	"WSAEREFUSED\n"
	"Database query was refused. \n"
	"A database query failed because it was actively refused.\n";
	 
	s_winsock_errors->operator[](WSAHOST_NOT_FOUND) = 
	"WSAHOST_NOT_FOUND\n"
	"Host not found. \n"
	"No such host is known. The name is not an official host name or alias, or it cannot be found in the database(s) \n"
	"being queried. This error may also be returned for protocol and service queries, and means that the specified \n"
	"name could not be found in the relevant database.\n";
	 
	s_winsock_errors->operator[](WSATRY_AGAIN) = 
	"WSATRY_AGAIN\n"
	"Nonauthoritative host not found. \n"
	"This is usually a temporary error during host name resolution and means that the local server did not receive a \n"
	"response from an authoritative server. A retry at some time later may be successful.\n";
	 
	s_winsock_errors->operator[](WSANO_RECOVERY) = 
	"WSANO_RECOVERY\n"
	"This is a nonrecoverable error. \n"
	"This indicates that some sort of nonrecoverable error occurred during a database lookup. This may be because \n"
	"the database files (for example, BSD-compatible HOSTS, SERVICES, or PROTOCOLS files) could not be found, \n"
	"or a DNS request was returned by the server with a severe error.\n";
	 
	s_winsock_errors->operator[](WSANO_DATA) = 
	"WSANO_DATA\n"
	"Valid name, no data record of requested type. \n"
	"The requested name is valid and was found in the database, but it does not have the correct associated data \n"
	"being resolved for. The usual example for this is a host name-to-address translation attempt \n"
	"(using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned \n"
	"but no A record-indicating the host itself exists, but is not directly reachable.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_RECEIVERS) = 
	"WSA_QOS_RECEIVERS\n"
	"QOS receivers. \n"
	"At least one QOS reserve has arrived.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_SENDERS) = 
	"WSA_QOS_SENDERS\n"
	"OS senders. \n"
	"At least one QOS send path has arrived.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_NO_SENDERS) = 
	"WSA_QOS_NO_SENDERS\n"
	"No QOS senders. \n"
	"There are no QOS senders.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_NO_RECEIVERS) = 
	"WSA_QOS_NO_RECEIVERS\n"
	"QOS no receivers. \n"
	"There are no QOS receivers.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_REQUEST_CONFIRMED) = 
	"WSA_QOS_REQUEST_CONFIRMED\n"
	"QOS request confirmed. \n"
	"The QOS reserve request has been confirmed.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_ADMISSION_FAILURE) = 
	"WSA_QOS_ADMISSION_FAILURE\n"
	"QOS admission error. \n"
	"A QOS error occurred due to lack of resources.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_POLICY_FAILURE) = 
	"WSA_QOS_POLICY_FAILURE\n"
	"QOS policy failure. \n"
	"The QOS request was rejected because the policy system couldn't allocate the requested \n"
	"resource within the existing policy. \n";
	 
	s_winsock_errors->operator[](WSA_QOS_BAD_STYLE) = 
	"WSA_QOS_BAD_STYLE\n"
	"QOS bad style. \n"
	"An unknown or conflicting QOS style was encountered.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_BAD_OBJECT) = 
	"WSA_QOS_BAD_OBJECT\n"
	"QOS bad object. \n"
	"A problem was encountered with some part of the filterspec or the provider-specific buffer in general.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_TRAFFIC_CTRL_ERROR) = 
	"WSA_QOS_TRAFFIC_CTRL_ERROR \n"
	"QOS traffic control error. \n"
	"An error with the underlying traffic control (TC) API as the generic QOS request was converted for local \n"
	"enforcement by the TC API. This could be due to an out of memory error or to an internal QOS provider error.\n" ;
	 
	s_winsock_errors->operator[](WSA_QOS_GENERIC_ERROR) = 
	"WSA_QOS_GENERIC_ERROR\n"
	"QOS generic error. \n"
	"A general QOS error.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_ESERVICETYPE) = 
	"WSA_QOS_ESERVICETYPE\n"
	"QOS service type error.\n" 
	"An invalid or unrecognized service type was found in the QOS flowspec.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFLOWSPEC) = 
	"WSA_QOS_EFLOWSPEC\n"
	"QOS flowspec error. \n"
	"An invalid or inconsistent flowspec was found in the QOS structure.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EPROVSPECBUF) = 
	"WSA_QOS_EPROVSPECBUF\n"
	"Invalid QOS provider buffer. \n"
	"An invalid QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFILTERSTYLE) = 
	"WSA_QOS_EFILTERSTYLE\n"
	"Invalid QOS filter style. \n"
	"An invalid QOS filter style was used.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFILTERTYPE) = 
	"WSA_QOS_EFILTERTYPE\n"
	"Invalid QOS filter type. \n"
	"An invalid QOS filter type was used.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFILTERCOUNT) = 
	"WSA_QOS_EFILTERCOUNT\n"
	"Incorrect QOS filter count. \n"
	"An incorrect number of QOS FILTERSPECs were specified in the FLOWDESCRIPTOR.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EOBJLENGTH) = 
	"WSA_QOS_EOBJLENGTH\n"
	"Invalid QOS object length. \n"
	"An object with an invalid ObjectLength field was specified in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFLOWCOUNT) = 
	"WSA_QOS_EFLOWCOUNT\n"
	"Incorrect QOS flow count. \n"
	"An incorrect number of flow descriptors was specified in the QOS structure.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EUNKOWNPSOBJ) = 
	"WSA_QOS_EUNKOWNPSOBJ\n"
	"Unrecognized QOS object. \n"
	"An unrecognized object was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EPOLICYOBJ) = 
	"WSA_QOS_EPOLICYOBJ\n"
	"Invalid QOS policy object. \n"
	"An invalid policy object was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EFLOWDESC) = 
	"WSA_QOS_EFLOWDESC\n"
	"Invalid QOS flow descriptor. \n"
	"An invalid QOS flow descriptor was found in the flow descriptor list.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EPSFLOWSPEC) = 
	"WSA_QOS_EPSFLOWSPEC\n"
	"Invalid QOS provider-specific flowspec. \n"
	"An invalid or inconsistent flowspec was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_EPSFILTERSPEC) = 
	"WSA_QOS_EPSFILTERSPEC\n"
	"Invalid QOS provider-specific filterspec. \n"
	"An invalid FILTERSPEC was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_ESDMODEOBJ) = 
	"WSA_QOS_ESDMODEOBJ\n"
	"Invalid QOS shape discard mode object. \n"
	"An invalid shape discard mode object was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_ESHAPERATEOBJ) = 
	"WSA_QOS_ESHAPERATEOBJ\n"
	"Invalid QOS shaping rate object. \n"
	"An invalid shaping rate object was found in the QOS provider-specific buffer.\n";
	 
	s_winsock_errors->operator[](WSA_QOS_RESERVED_PETYPE) = 
	"WSA_QOS_RESERVED_PETYPE\n"
	"Reserved policy QOS element type. \n"
	"A reserved policy element was found in the QOS provider-specific buffer.\n";

}
