Got it working, test sample below (runs in a separate thread):
public void Listen()
{
int ByteRead;
IPAddress ipAddress = Dns.GetHostAddresses("localhost")[0];
TcpListener tcpListener = new TcpListener(IPAddress.Any, 399);
TcpClient tcpClient;
#if CONSOLE_DEBUG
Console.WriteLine("Socket created.");
#endif
while (true)
{
#if CONSOLE_DEBUG
Console.WriteLine("Listener created.");
#endif
while (true)
{
try
{
tcpListener.Start();
}
catch ( SocketException se)
{
Console.WriteLine(se.Message);
}
tcpClient = tcpListener.AcceptTcpClient();
NetworkStream ns = tcpClient.GetStream();
while ((ByteRead = ns.ReadByte()) <= 0) { };
Console.WriteLine("Length = {0}", ByteRead);
string TempStr = new string(' ', 0);
for (int loop = 0; loop < ByteRead; loop++)
{
TempStr += (char)ns.ReadByte();
}
Console.WriteLine("\"{0}\" received", TempStr);
lock (TextBox1)
{
}
ns.Close();
}
tcpClient.Close();
Console.WriteLine("Listen ended.");
}
}