' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Collections.Generic
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Namespace E
    Public Class AClient
        Private cli As Socket
        Private buf As Byte()
        Private buflen As Integer
        Public Sub New(cli As Socket, srv As AServer)
            Console.WriteLine("Connection open from " & Convert.ToString(cli.RemoteEndPoint))
            Me.cli = cli
            buf = New Byte(10239) {}
            buflen = 0
            cli.BeginReceive(buf, buflen, buf.Length - buflen, SocketFlags.None, New AsyncCallback(AddressOf GotData), srv)
        End Sub
        Public Sub GotData(res As IAsyncResult)
            Dim n As Integer = cli.EndReceive(res)
            Console.WriteLine("Read " & n & " bytes from " & Convert.ToString(cli.RemoteEndPoint))
            If n > 0 Then
                buflen += n
                Dim lf As Integer = Array.IndexOf(buf, CByte(AscW(ControlChars.Lf)), 0, buflen)
                If lf >= 0 Then
                    lf += 1
                    DirectCast(res.AsyncState, AServer).SendAll(buf, lf, Me)
                    If buflen > lf Then
                        Array.Copy(buf, lf, buf, 0, buflen - lf)
                    End If
                    buflen -= lf
                End If
                cli.BeginReceive(buf, buflen, buf.Length - buflen, SocketFlags.None, New AsyncCallback(AddressOf GotData), DirectCast(res.AsyncState, AServer))
            Else
                Console.WriteLine("Connection closed from " & Convert.ToString(cli.RemoteEndPoint))
                DirectCast(res.AsyncState, AServer).Remove(Me)
            End If
        End Sub
        Public Sub Send(buf As Byte(), buflen As Integer)
            Console.WriteLine("Write " & buflen & " bytes to " & Convert.ToString(cli.RemoteEndPoint))
            cli.Send(buf, buflen, SocketFlags.None)
        End Sub
    End Class
    Public Class AServer
        Private cli As List(Of AClient)
        Private srv As Socket
        Public Sub New(port As Integer)
            cli = New List(Of AClient)()
            srv = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            srv.Bind(New IPEndPoint(IPAddress.Any, port))
            srv.Listen(10)
            srv.BeginAccept(New AsyncCallback(AddressOf GotConnection), srv)
        End Sub
        Public Sub GotConnection(res As IAsyncResult)
            cli.Add(New AClient(srv.EndAccept(res), Me))
            srv.BeginAccept(New AsyncCallback(AddressOf GotConnection), srv)
        End Sub
        Public Sub Remove(acli As AClient)
            cli.Remove(acli)
        End Sub
        Public Sub SendAll(buf As Byte(), buflen As Integer, sender As AClient)
            Console.WriteLine("Write " & buflen & " bytes to all")
            For Each acli As AClient In cli
                If sender IsNot acli Then
                    acli.Send(buf, buflen)
                End If
            Next
        End Sub
        Public Sub Close()
            srv.Close()
        End Sub
    End Class
    Public Class Program
        Public Shared Sub Main(args As String())
            Dim server As New AServer(1234)
            Console.ReadKey()
            server.Close()
        End Sub
    End Class
End Namespace
