Public Class SocketMaster
Public Socket As Socket = Nothing
Private Form As frmMain
Public Sub New(ByVal frm As Form)
Form = DirectCast(frm, frmMain)
End Sub
Public Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
Dim hostEntry As IPHostEntry = Nothing
hostEntry = Dns.Resolve(server)
Dim address As IPAddress
For Each address In hostEntry.AddressList
Dim endPoint As New IPEndPoint(address, port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
Try
tempSocket.Connect(endPoint)
Catch Err As SocketException
AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
End Try
If tempSocket.Connected Then
Socket = tempSocket
Exit For
End If
Next address
Return True
End Function
Public Function Disconnect() As Boolean
Try
Socket.Close()
Catch Err As SocketException
AddChat(Form.rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
End Try
Return True
End Function
End Class