Do you really want to write it for yourself?
Since there are lots of free tools outside, able to tell you this. See
Sysinternals TCPview[
^] for example, or the built-in windows tool
NETSTAT[
^].
But it is really not complicated at all, check out this article:
http://towardsnext.wordpress.com/2009/02/09/netstat-in-c/[
^]
[Update]
Here is the vb.net version of it:
Imports System
Imports System.Net
Imports System.Net.NetworkInformation
Module Program
Sub Main()
Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
Dim endPoints As IPEndPoint() = ipProperties.GetActiveTcpListeners()
Dim tcpConnections As TcpConnectionInformation() = ipProperties.GetActiveTcpConnections()
For Each info As TcpConnectionInformation In tcpConnections
Console.WriteLine("Local : " & info.LocalEndPoint.Address.ToString() & ":" & info.LocalEndPoint.Port.ToString() & vbLf & "Remote : " & info.RemoteEndPoint.Address.ToString() & ":" & info.RemoteEndPoint.Port.ToString() & vbLf & "State : " & info.State.ToString() & vbLf & vbLf)
Next
Console.ReadLine()
End Sub
End Module