I think this one may be hard as applications/services can use many different ports, based on how the are written. If the said application/service is not running at the time you "checked" the port you would not know that is the one it uses.
Ports open and close so catching them may be the biggest challenge.
You can add a Try / Catch block. If it fails switch to an alternative port. The problem is that you will need to also switch to the alternative port in the client.
This is how the designer works for example. The IDE tries to connect to one port and if it fails it tries to connect to the second port.
Dim MyPort as int = 2020
Dim Sw As Boolean = False
Do While Sw = False
Try
SvrSocket1.Initialize(MyPort, "SvrSocket1")
Sw = True
Catch
MyPort = MyPort + 1
End Try
Loop