

Namespace System
Namespace Net
Namespace Sockets

TypeDef SocketException = ActiveBasic.Windows.WindowsException

Enum AddressFamily
	AppleTalk         = PF_APPLETALK
	Atm               = PF_ATM
	Banyan            = PF_BAN
	Ccitt             = PF_CCITT
	Chaos             = PF_CHAOS
'	Cluster
	DataKit           = PF_DATAKIT
'	DataLink
	DecNet            = PF_DECnet
	Ecma              = PF_ECMA
	FireFox           = PF_FIREFOX
'	HyperChannel
'	Ieee12844
	ImpLink           = PF_IMPLINK
	InterNetwork      = PF_INET
	InterNetworkV6    = PF_INET6
	Ipx               = PF_IPX
'	Irda
	Iso               = PF_ISO
	Lat               = PF_LAT
'	Max
'	NetBios
'	NetworkDesigners
	NS                = PF_NS
	Osi               = PF_OSI
	Pup               = PF_PUP
	Sna               = PF_SNA
	Unix              = PF_UNIX
	Unknown           = PF_UNKNOWN1
	Unspecified       = PF_UNSPEC
	VoiceView         = PF_VOICEVIEW
End Enum

Enum SocketType
	Stream    = SOCK_STREAM
	Dgram     = SOCK_DGRAM
	Raw       = SOCK_RAW
	Rdm       = SOCK_RDM
	Seqpacket = SOCK_SEQPACKET
End Enum

Enum ProtocolType
	Ggp                                = IPPROTO_GGP
	Icmp                               = IPPROTO_ICMP
	IcmpV6                             = IPPROTO_ICMPV6
	Idp                                = IPPROTO_IDP
	Igmp                               = IPPROTO_IGMP
	IP                                 = IPPROTO_IP
	IPSecAuthenticationHeader          = IPPROTO_AH
	IPSecEncapsulatingSecurityPayload  = IPPROTO_ESP
	IPv4                               = IPPROTO_IPV4
	IPv6                               = IPPROTO_IPV6
	IPv6DestinationOptions             = IPPROTO_DSTOPTS
	IPv6FragmentHeader                 = IPPROTO_FRAGMENT
'	IPv6HopByHopOptions
	IPv6NoNextHeader                   = IPPROTO_NONE
	IPv6RoutingHeader                  = IPPROTO_ROUTING
	Ipx
	ND                                 = IPPROTO_ND
	Pup                                = IPPROTO_PUP
	Raw                                = IPPROTO_RAW
'	Spx
'	SpxII
	Tcp                                = IPPROTO_TCP
	Udp                                = IPPROTO_UDP
'	Unknown
'	Unspecified
End Enum

Enum SocketFlags
	None                    = 0
	OutOfBand               = MSG_OOB
	Peek                    = MSG_PEEK
	DontRoute               = MSG_DONTROUTE
'	MaxIOVectorLength
'	Truncated
'	ControlDataTruncated
'	Broadcast
'	Multicast
	Pertical                = MSG_PARTIAL
End Enum

/*!
@brief  Winsockを初期化するクラス
@author OverTaker
@date   2008/10/05
*/
Class Winsock
	Static wsa As WSADATA
Public

	/*!
	@brief  Winsockの初期化を行う
	*/
	Static Sub Initialize()
		Initialize(New Version(2, 2))
	End Sub

	/*!
	@brief  Winsockを初期化する
	@param  Winsockを初期化するバージョン
	*/
	Static Sub Initialize(version As Version)
		If WSAStartup(MAKEWORD(version.Major, version.Minor), wsa) Then
			Throw New SocketException(WSAGetLastError(), "Winsock.Initalize")
		End If
	End Sub

	/*!
	@brief  Winsockの終了処理を行う
	*/
	Static Sub Finalize()
		If WSACleanup() = SOCKET_ERROR Then
			Throw New SocketException(WSAGetLastError(), "Winsock.Finalize")
		End If
	End Sub

	/*!
	@brief  Winsockのバージョンを取得します
	@return バージョン
	*/
	Static Function Version() As Version
		Return New System.Version(HIBYTE(wsa.wVersion), LOBYTE(wsa.wVersion))
	End Function

	/*!
	@brief  OSで扱える最大のWinsockバージョンを取得します
	@return バージョン
	*/
	Static Function HighVersion() As Version
		Return New System.Version(HIBYTE(wsa.wHighVersion), LOBYTE(wsa.wHighVersion))
	End Function

	/*!
	@brief  Winsockの実装の説明を取得する
	@return 説明
	*/
	Static Function Description() As String
		Return New String(wsa.szDescription As PCTSTR)
	End Function

	/*!
	@brief  Winsockの状態を取得する
	@return 状態
	*/
	Static Function SystemStatus() As String
		Return New String(wsa.szSystemStatus As PCTSTR)
	End Function
End Class

End Namespace
End Namespace
End Namespace