I am using VB .Net to explore Sockets for the first time. My task is simple: create a socket, connect to a web server, download the stream, display it in a rich textbox, disconnect, connect to another server.

My problem: I can't do the last part of the tast 'connect to another server.'

When I try to reconnect, I get the following error:

System.ObjectDisposedException: Cannot access a disposed object named "System.Net.Sockets.Socket".
Object name: "system.Net.Sockets.Socket".
at System.Sockets.Socket.Connect(EndPoint remoteEP)
at LearningVBData.sockclient.btnConnect_Click(Object sender, EventArgs e) in [source location]
line 122

How can I close a Socket, and reconnect to a new or the same location using the same Socket?

VB Code:
  1. 'Global declarations
  2.     Dim strRetPage As String = Nothing
  3.     Dim server As String = Nothing
  4.  
  5.     'Creates the Socket for sending data over TCP.
  6.     Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  7.  
  8.  
  9.     Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
  10.         server = txtServer.Text
  11.         Dim hostadd As System.Net.IPAddress = System.Net.Dns.Resolve(server).AddressList(0)
  12.         Dim EPhost As New System.Net.IPEndPoint(hostadd, 80)
  13.  
  14.         Try
  15.             sock.Connect(EPhost)
  16.         Catch ex As Exception
  17.             MsgBox("Socket error: " & ex.ToString(), vbExclamation)
  18.         End Try
  19.  
  20.         If Not sock.Connected Then
  21.             MsgBox("Socket error: " _
  22.                 & Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()), vbExclamation)
  23.         Else
  24.             Me.Text = "Sockclient - Connected"
  25.         End If
  26.  
  27.     End Sub
  28.  
  29.     Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
  30.         Try
  31.             sock.Shutdown(SocketShutdown.Both)
  32.         Catch ex As Exception
  33.             MsgBox("Socket error: " & ex.ToString(), vbExclamation)
  34.         End Try
  35.  
  36.         sock.Close()
  37.  
  38.         If sock.Connected Then
  39.             MsgBox("Socket error: " _
  40.                 & Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()), vbExclamation)
  41.         Else
  42.             Me.Text = "Sockclient - Disconnected"
  43.         End If
  44.     End Sub
  45.  
  46.     Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
  47.         Dim ASCII As System.Text.Encoding = System.Text.Encoding.ASCII
  48.         Dim StrGet As String = "GET / HTTP/1.1" & ControlChars.CrLf & _
  49.                             "Host: " & server & ControlChars.CrLf & _
  50.                             "Connection: Close" & ControlChars.CrLf & _
  51.                             ControlChars.CrLf
  52.         Dim ByteGet As Byte() = ASCII.GetBytes(StrGet)
  53.         Dim RecvBytes(256) As Byte
  54.  
  55.         Try
  56.             ' Sends the GET text to the host
  57.             sock.Send(ByteGet, ByteGet.Length, 0)
  58.         Catch ex As Exception
  59.             MsgBox("Socket error: " & ex.ToString(), vbExclamation)
  60.             Return
  61.         End Try
  62.  
  63.         ' Receives the page, looping until all bytes are received
  64.         Dim bytes As Int32 = sock.Receive(RecvBytes, RecvBytes.Length, 0)
  65.         strRetPage = "Default HTML page on " & server & ":" & ControlChars.CrLf
  66.         strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
  67.  
  68.         While bytes > 0
  69.             bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0)
  70.             strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
  71.         End While
  72.  
  73.         txtData.Text = strRetPage
  74.  
  75.     End Sub