Per my message; here's the rough code you can attempt to pull apart and/or finish. It was too large to copy and there's no attachment feature. In 2005, you don't have to do alot of this (mainly; inheriting the arraylist and creating your own collection object. Deffinitely use a Generic instead). I started writing it in 1.1 and don't have the old IDE anymore to finish it:
VB Code:
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Imports System
  5. Imports System.Net
  6. Imports System.Net.Sockets
  7.  
  8. Public Class SocketClients
  9.     Inherits ArrayList
  10.  
  11.     Protected Shared RequestPort As Int32
  12.     Private _PortRange(0) As Port
  13.     Private _ReqListener As Net.Sockets.TcpListener
  14.     Private _LocalEP As Net.IPEndPoint
  15.     Private _thrdPend As New Threading.Thread(AddressOf WaitForPending)
  16.  
  17.     Public Event NewClient(ByVal obj As Client)
  18.  
  19.     Private Structure Port
  20.         Public PortNum As Int32
  21.         Public avaliable As Boolean
  22.         Public Sub New(ByVal num As Int32, ByVal avail As Boolean)
  23.             PortNum = num
  24.             avaliable = avail
  25.         End Sub
  26.     End Structure
  27.  
  28.     Public Sub New(ByVal RequestPortIn As Int32, ByVal PortStart As Int32, ByVal PortEnd As Int32)
  29.         MyBase.New()
  30.         If RequestPort >= PortStart And RequestPort <= PortEnd Then
  31.             Throw New ArgumentException("Request port cannot be in the Port pool", "RequestPort")
  32.         ElseIf PortStart > PortEnd Then
  33.             Throw New ArgumentException("PortStart cannot be prior to PortEnd", "PortStart")
  34.         ElseIf PortStart < 49152 Then
  35.             Throw New ArgumentException("""Well-Known"" and ""DCCP Well-Known"" ports cannot be used dynamically.", "PortStart")
  36.         ElseIf PortEnd > 65535 Then
  37.             Throw New ArgumentException("Requested Range is out of scope.", "PortEnd")
  38.         Else
  39.             Try
  40.                 For i As Int32 = PortStart To PortEnd
  41.                     _PortRange(UBound(_PortRange)) = New Port(i, True)
  42.                     If Not i = PortEnd Then
  43.                         ReDim Preserve _PortRange(_PortRange.Length)
  44.                     End If
  45.                 Next i
  46.  
  47.                 RequestPort = RequestPortIn
  48.                 _LocalEP = New Net.IPEndPoint(Dns.Resolve(Dns.GetHostName).AddressList(0), RequestPort)
  49.                 _ReqListener = New Net.Sockets.TcpListener(_LocalEP)
  50.             Catch ex As Exception
  51.                 Stop        'XXX DEBUG XXX
  52.             End Try
  53.         End If
  54.     End Sub
  55.  
  56.     Public Sub StartListen()
  57.         _ReqListener.Start()
  58.         Select Case _thrdPend.ThreadState
  59.             Case Threading.ThreadState.Unstarted
  60.                 _thrdPend.Start()
  61.             Case Threading.ThreadState.Running
  62.                 'Do nothing, already running
  63.             Case Threading.ThreadState.Suspended
  64.                 _thrdPend.Resume()
  65.             Case Threading.ThreadState.SuspendRequested
  66.                 _thrdPend.Resume()
  67.        End Select
  68.     End Sub
  69.  
  70.     Public Sub StopListen()
  71.         _ReqListener.Stop()
  72.         _thrdPend.Abort()
  73.     End Sub
  74.  
  75.     Private Sub WaitForPending()
  76.         While Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.StopRequested Or _
  77.             Threading.Thread.CurrentThread.ThreadState <> Threading.ThreadState.AbortRequested
  78.             If Threading.Thread.CurrentThread.ThreadState = Threading.ThreadState.SuspendRequested Then
  79.                 Threading.Thread.CurrentThread.Suspend()
  80.             Else
  81.                 If _ReqListener.Pending Then
  82.                     SyncLock Threading.Thread.CurrentThread     'Begin Negotiation
  83.                         Dim socTemp As Net.Sockets.Socket = _ReqListener.AcceptSocket
  84.                         Dim holdPort As Port
  85.                         Dim buffer() As Byte
  86.  
  87.                         For i As Int32 = 0 To UBound(_PortRange, 1) - 1
  88.                             If _PortRange(i).avaliable = True Then 'According to our system, it's free
  89.                                 If validateport(_PortRange(i).PortNum) Then
  90.                                     holdPort = _PortRange(i)
  91.                                     _PortRange(i).avaliable = False
  92.                                     i = UBound(_PortRange, 1) - 1   'Seems more clever then an "Exit For"
  93.                                 Else                        'Some other program must have it, reflect in our array and continue
  94.                                     _PortRange(i).avaliable = False
  95.                                 End If
  96.                             End If
  97.                         Next i
  98.  
  99.                         If holdPort.PortNum = 0 Then
  100.                             buffer = Text.Encoding.ASCII.GetBytes("NOAVAIL")
  101.                         Else
  102.                             Dim client As New Client(socTemp.RemoteEndPoint.Serialize.Item(4) & "." & _
  103.                                 socTemp.RemoteEndPoint.Serialize.Item(5) & "." & _
  104.                                 socTemp.RemoteEndPoint.Serialize.Item(6) & "." & _
  105.                                 socTemp.RemoteEndPoint.Serialize.Item(7), holdPort.PortNum)
  106.                              buffer = Text.Encoding.ASCII.GetBytes("S:" & holdPort.PortNum)
  107.                             Me.Add(client, True)
  108.                         End If
  109.                         socTemp.Send(buffer, buffer.Length, SocketFlags.None)
  110.                         socTemp.Shutdown(SocketShutdown.Receive)
  111.                     End SyncLock
  112.                 End If
  113.             End If
  114.         End While
  115.  
  116.         Select Case Threading.Thread.CurrentThread.ThreadState
  117.  
  118.             Case Threading.ThreadState.AbortRequested
  119.  
  120.                 Threading.Thread.CurrentThread.Abort()
  121.  
  122.         End Select
  123.  
  124.     End Sub