I try to test TCP connection with the following code.
System.Threading.Thread t = new System.Threading.Thread(() =>
{
using (TcpClient client =
Much too late to be of use to the OP but for anybody else still finding this page from a search, you can solve this using the asynchronous programming features introduced in .Net 4.5.
var hostname = "127.0.0.1";
var port = 123;
var timeout = TimeSpan.FromSeconds(3);
var client = new TcpClient();
if (!client.ConnectAsync(hostname, port).Wait(timeout))
{
// timed out
}