/*
#ifdef UNICODE
Const _FuncName_getaddrinfo = "GetAddrInfoW"
Const _FuncName_freeaddrinfo = "FreeAddrInfoW"
Const _FuncName_getnameinfo = "GetNameInfoW"
Const _FuncName_inet_pton = "InetPtonW"
Const _FuncName_inet_ntop = "InetNtopW"
#else
Const _FuncName_getaddrinfo = "GetAddrInfoA"
Const _FuncName_freeaddrinfo = "FreeAddrInfoA"
Const _FuncName_getnameinfo = "GetNameInfoA"
Const _FuncName_inet_pton = "InetPton"
Const _FuncName_inet_ntop = "InetNtop"
#endif
*/

Const _FuncName_getaddrinfo = "getaddrinfo"
Const _FuncName_freeaddrinfo = "freeaddrinfo"
Const _FuncName_getnameinfo = "getnameinfo"

Const INET_ADDRSTRLEN = 22
Const INET6_ADDRSTRLEN = 65

Const EAI_AGAIN     = WSATRY_AGAIN
Const EAI_BADFLAGS  = WSAEINVAL
Const EAI_FAIL      = WSANO_RECOVERY
Const EAI_FAMILY    = WSAEAFNOSUPPORT
Const EAI_MEMORY    = WSA_NOT_ENOUGH_MEMORY
'Const EAI_NODATA   =  WSANO_DATA
Const EAI_NONAME    = WSAHOST_NOT_FOUND
Const EAI_SERVICE   = WSATYPE_NOT_FOUND
Const EAI_SOCKTYPE  = WSAESOCKTNOSUPPORT

Const EAI_NODATA    = EAI_NONAME

Type addrinfo
	ai_flags As Long
	ai_family As Long
	ai_socktype As Long
	ai_protocol As Long
	ai_addrlen As DWord
	ai_canonname As PTSTR
	ai_addr As *sockaddr
	ai_next As *addrinfo
End Type

Const AI_PASSIVE     = &H00000001  ' Socket address will be used in bind() call
Const AI_CANONNAME   = &H00000002  ' Return canonical name in first ai_canonname
Const AI_NUMERICHOST = &H00000004  ' Nodename must be a numeric address string
Const AI_NUMERICSERV = &H00000008  ' Servicename must be a numeric port number

Const AI_ALL         = &H00000100  ' Query both IP6 and IP4 with AI_V4MAPPED
Const AI_ADDRCONFIG  = &H00000400  ' Resolution only if global address configured
Const AI_V4MAPPED    = &H00000800  ' On v6 failure, query v4 and convert to V4MAPPED format


Const AI_NON_AUTHORITATIVE       = &H4000   ' LUP_NON_AUTHORITATIVE
Const AI_SECURE                  = &H8000   'LUP_SECURE 
Const AI_RETURN_PREFERRED_NAMES  = &H10000  ' LUP_RETURN_PREFERRED_NAMES

TypeDef socklen_t = Long

Declare Function getaddrinfo Lib "Ws2_32" Alias _FuncName_getaddrinfo (pNodeName As LPCTSTR, pServiceName As LPCTSTR, ByRef pHints As addrinfo, ByRef ppResult As *addrinfo) As Long
Declare Sub freeaddrinfo Lib "Ws2_32" Alias _FuncName_freeaddrinfo (ByRef ai As addrinfo)
Declare Function getnameinfo Lib "Ws2_32" Alias _FuncName_getnameinfo (ByRef sa As sockaddr, salen As socklen_t, host As LPTSTR, hostlen As SIZE_T, serv As LPTSTR, servlen As SIZE_T, flags As Long) As Long

/*
Declare Function inet_pton Lib "Ws2_32" Alias _FuncName_inet_pton (family As Long, pszAddrString As LPCTSTR, pAddrBuf As VoidPtr) As Long
Declare Function inet_ntop Lib "Ws2_32" Alias _FuncName_inet_ntop (family As Long, pAddr As VoidPtr, pStringBuf As LPTSTR, StringBufSize As SIZE_T) As LPCTSTR
*/

Const GAI_STRERROR_BUFFER_SIZE = 1024
Function gai_strerror( ecode As Long) As LPCTSTR
	Dim dwMsgLen As DWord
	Dim buff[GAI_STRERROR_BUFFER_SIZE] As SByte
	dwMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS,
                             NULL,
                             ecode,
                             LANG_USER_DEFAULT,
                             buff,
                             GAI_STRERROR_BUFFER_SIZE,
                             NULL)
	Return buff
End Function

Const NI_MAXHOST = 1025  /* Max size of a fully-qualified domain name */
Const NI_MAXSERV = 32    /* Max size of a service name */

/* Flags for getnameinfo() */

Const NI_NOFQDN       = &H01  /* Only return nodename portion for local hosts */
Const NI_NUMERICHOST  = &H02  /* Return numeric form of the host's address */
Const NI_NAMEREQD     = &H04  /* Error if the host's name not in DNS */
Const NI_NUMERICSERV  = &H08  /* Return numeric form of the service (port #) */
Const NI_DGRAM        = &H10  /* Service is a datagram service */
