I have the following code for the server application:

```
        TcpListener recSock = new TcpListener(400);
        recSock.Start();
        TcpClient client = recSock.AcceptTcpClient();
        NetworkStream netStream = client.GetStream();

        Byte[] data = new Byte[256];

        int i;

        while((i = netStream.Read(data, 0, data.Length)) != 0) {
            string cmd = ASCIIEncoding.ASCII.GetString(data, 0, i);
            Console.WriteLine(cmd);
            if(cmd == "R") {
                RestartScheduler();
            }
        }

        client.Close();

```

And the client looks like:

```
        TcpClient client = new TcpClient();
        client.Connect("VM-SCHEDULER", 400);
        NetworkStream netStream = client.GetStream();
        Byte[] data = ASCIIEncoding.ASCII.GetBytes("R");
        netStream.Write(data, 0, data.Length);
        netStream.Flush();
        client.Close();

```

All is fine the first time the client connects the "R" command is read and the RestartScheduler() method is called, however all subsequent commands fail until the server is restarted.

I have used telnet to connect to the server after the first attempt and it is still listening on the port.

Where am i going wrong?

EDIT:

Basically what I am trying to accomplish is that the server will listen always, a client will then connect send the "R" command then close the connection. The client must be able to connect to the server at any time to issue the command. There will only be 1 client at any given time.