Im creating a simple program. (soon to be vastly complex) that is connecting to a server, and sending an authentication code to connect. I got the connection part, but what keeps breaking up is the method of authenticating. I have the entire authentication process in a while loop, so that it will keep going untill it recives the authentication reply from the server (an OK-002938, or what ever client number the server dicides.

This is my client code for the event:

Private Sub CONNECTSERVER_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles CONNECTSERVER.DoWork
            If network_client.Connected = False Then
                network_client.Client.Connect(My.Settings.service_address.ToString, My.Settings.service_port.ToString)
                If network_client.Client.Connected Then
                    status_connected.Text = "Conneected"
                    vox.Speak("Connected to Homation service.")
                    Dim client_authenticate As String = Nothing
                    While client_authenticate = Nothing

                        Dim networkStream As NetworkStream = network_client.GetStream()
                        Dim bytes(network_client.ReceiveBufferSize) As Byte
                        networkStream.Read(bytes, 0, CInt(network_client.ReceiveBufferSize))
                        ' Return the data received from the client to the console.
                        Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                        If clientdata.ToString.ToUpper = "AUTHENTICATE" Then
                            'The server wants us to authenticate. Send credentials
                            network_client.Client.Send(Encoding.ASCII.GetBytes("AUTH " & My.Settings.Service_key.ToString), Encoding.ASCII.GetByteCount("AUTH " & My.Settings.Service_key.ToString), SocketFlags.None)

                        End If
                    End While

                End If
                'ping the server :)
                network_client.Client.Send(Encoding.ASCII.GetBytes("ping." & IPAddress()), Encoding.ASCII.GetByteCount("ping." & IPAddress()), SocketFlags.None)

            End If

        Catch ex As Exception

        End Try

    End Sub

And this is the code for the server

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            'Accept the pending client connection and return 
            'a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = server.AcceptTcpClient()
            update_networking("Connection Accepted")
            'SEND DATA
            'Request the authentication key from client
            tcpClient.Client.Send(Encoding.ASCII.GetBytes("AUTHENTICATE"), Encoding.ASCII.GetByteCount("AUTHENTICATE"), SocketFlags.None)
            Dim client_authenticate As String = "NULLED EXCEPTION"
            While client_authenticate = "NULLED EXCEPTION"
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
             ' >> errored Line   server.Server.Receive(bytes, CInt(tcpClient.ReceiveBufferSize), SocketFlags.None)
                Dim clientdata As String = Encoding.ASCII.GetString(byte)
                If clientdata.ToString.Contains("AUTH") Then
                    'The client has sent some authentication material
                    Dim credential As String
                    credential = clientdata.Remove(0, 4)

                End If

            End While
            'Send data and authenticate before closing connection
        Catch ex As Exception

        End Try
    End Sub

The client side is flawles. but the server side, works, but it doesnt continue past storing the bytes into a container...

here is codeProject article on multi-threaded application.

here is codeProject article on multi-threaded application.

These two applications are already separate. I have to do them seperatly, my end product is separate from the server.