The TcpClient.Connect
method is synchronous and as such will block the calling thread until the connect operation has completed either successfully or with an error.
If the calling thread happens to be the thread that owns the window handle then the window will appear unresponsive.
In order to solve your problem you could use the asynchronous counterpart to the synchronous connect method that will carry out the connect operation on a separate worker thread.
labelConnectionState.Text = "Connecting";
Connectionclient.BeginConnect("..", 43594, ConnectCallback, Connectionclient);
...
private static void ConnectCallback(IAsyncResult asyncResult)
{
try
{
TcpClient Connectionclient = (TcpClient) asyncResult.AsyncState;
Connectionclient.EndConnect(asyncResult);
labelConnectionState.Text = "Connected";
}
catch (SocketException socketException)
{
labelConnectionState.Text = "Server unavailable";
}
}