This post is about home-made netcat :) Of course this is only one function of it, but it can be useful too. Sometime you want to listen on some port and see data that arrives to it to analyze it.  There is the script:

function Trace-Port([int]$port=23, [string]$IPAdress="127.0.0.1", [switch]$Echo=$false){
    $listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAdress), $port)
    $listener.start()
    [byte[]]$bytes = 0..255|%{0}
    write-host "Waiting for a connection on port $port..."
    $client = $listener.AcceptTcpClient()
    write-host "Connected from $($client.Client.RemoteEndPoint)"
    $stream = $client.GetStream()
    while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
    {
        $bytes[0..($i-1)]|%{$_}
        if ($Echo){$stream.Write($bytes,0,$i)}
    }
    $client.Close()
    $listener.Stop()
    write-host "Connection closed."
}

After you add this function to your shell you can use it this way:

PS> Trace-Port -ip 192.168.1.99 -port 333
Waiting for a connection on port 333...

Now script waiting for connection on port 333. I will connect to this port using telnet.exe, and then write word "Test" into it:

Connected from 192.168.1.99:61829
84
101
115
116
13
10
Connection closed.

Function outputs byte array, so if you want to see characters you can pipe output to Foreach-Object cmd-let and convert it to chars:

PS> Trace-Port 123 | foreach { write-host ([char]$_) -NoNewLine }
Waiting for a connection on port 123...
Connected from 127.0.0.1:62174
Test
Connection closed.

You can also specify -Echo switch if you want all input echoed back to client.

Because native PowerShell doesnt support threads, this script will run in same thread as PowerShell, and will not respond to events like Ctrl+C until client disconnect. But this problem can be solved by using PSEventing library, and i will try to rewrite this script when i will have free time again :)