void RNS2_Berkley::GetSystemAddressIPV4And6 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut )
{
#if RAKNET_SUPPORT_IPV6==1

	socklen_t slen;
	sockaddr_storage ss;
	slen = sizeof(ss);

	if ( getsockname__(rns2Socket, (struct sockaddr *)&ss, &slen)!=0)
	{
#if defined(_WIN32) && defined(_DEBUG)
		DWORD dwIOError = GetLastError();
		LPVOID messageBuffer;
		FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
			( LPTSTR ) & messageBuffer, 0, NULL );
		// something has gone wrong here...
		RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer );

		//Free the buffer.
		LocalFree( messageBuffer );
#endif
		systemAddressOut->FromString(0);
		return;
	}

	if (ss.ss_family==AF_INET)
	{
		memcpy(&systemAddressOut->address.addr4,(sockaddr_in *)&ss,sizeof(sockaddr_in));
		systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr4.sin_port);

		uint32_t zero = 0;		
		if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0)
			systemAddressOut->SetToLoopback(4);
		//	systemAddressOut->address.addr4.sin_port=ntohs(systemAddressOut->address.addr4.sin_port);
	}
	else
	{
		memcpy(&systemAddressOut->address.addr6,(sockaddr_in6 *)&ss,sizeof(sockaddr_in6));
		systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr6.sin6_port);

		char zero[16];
		memset(zero,0,sizeof(zero));
		if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0)
			systemAddressOut->SetToLoopback(6);

		//	systemAddressOut->address.addr6.sin6_port=ntohs(systemAddressOut->address.addr6.sin6_port);
	}

#else
	(void) rns2Socket;
	(void) systemAddressOut;
	return;
#endif
}
void RNS2_Berkley::GetSystemAddressIPV4 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut )
{
    sockaddr_in sa;
    memset(&sa,0,sizeof(sockaddr_in));
    socklen_t len = sizeof(sa);
    //int r =
        getsockname__(rns2Socket, (sockaddr*)&sa, &len);
    systemAddressOut->SetPortNetworkOrder(sa.sin_port);
    systemAddressOut->address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr;

    if (systemAddressOut->address.addr4.sin_addr.s_addr == INADDR_ANY)
    {
            systemAddressOut->address.addr4.sin_addr.s_addr=inet_addr__("127.0.0.1");
    }
}
void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
{
	int idx = 0;

	int rns2Socket = socket__(AF_INET, SOCK_DGRAM, IPPROTO_IP);
	if (rns2Socket >= 0)
	{
		// A connected socket is required to get the true ip address
		// otherwise we are likely to just get 127.0.0.1
		// Connect to the public google DNS server
		sockaddr_in saGoogle;
		memset(&saGoogle,0,sizeof(sockaddr_in));
		saGoogle.sin_family = AF_INET;
		saGoogle.sin_addr.s_addr=inet_addr__("8.8.8.8");
		saGoogle.sin_port = htons(53);
		connect__(rns2Socket, (const sockaddr*) &saGoogle, sizeof(saGoogle));

		sockaddr_in sa;
		memset(&sa,0,sizeof(sockaddr_in));
		socklen_t len = sizeof(sa);
		getsockname__(rns2Socket, (sockaddr*)&sa, &len);

		addresses[idx].address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr;
		idx++;

		// Disconnect the socket after getting the address
		memset(&saGoogle,0,sizeof(sockaddr_in));
		connect__(rns2Socket, (const sockaddr*) &saGoogle, sizeof(saGoogle));
		closesocket__(rns2Socket);
	}

	while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
	{
		addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
		idx++;
	}
}