Попробуйте этот пример кода
Dim _tcpClient As New TcpClient
Dim _networkStream As NetworkStream
Dim _Msg As String
With _tcpClient
.Connect(Me.txtServerIp.Text, Integer.Parse(Me.txtPortNum.Text))
_networkStream = .GetStream
Dim sw As New StreamWriter(_networkStream)
Dim sr As New StreamReader(_networkStream)
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine(String.Format("USER {0}", Me.txtUsername.Text))
sw.Flush()
End If
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine(String.Format("PASS {0}", Me.txtPassword.Text))
sw.Flush()
End If
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine("STAT ")
sw.Flush()
End If
_Msg = sr.ReadLine
Dim MsgCount As String = _Msg.Split(New String() {" "}, _
StringSplitOptions.RemoveEmptyEntries)(1)
If Integer.Parse(Me.lblMsgCount.Text) < Integer.Parse(MsgCount) Then
Me.lblMsgCount.Text = MsgCount
End If
sw.WriteLine("Quit ")
sw.Flush()
sw.Close()
sr.Close()
_networkStream.Close()
_tcpClient.Close()
End With