If you use TCP connection for each connected client, each clientSocket represents one connection to one client. Hence, from the piece of code you show above, it just sends message to one client, which client get the message depends on what you hold in clientSocket variable (in this case, your problem is sending only to the last connected client). To send a message to all clients, you must repeat the write function call for each connected client. Below is a piece of code for sending message to each client (given by the clientSocket variable):
public void sendMessage(Socket clientSocket, string _msg)
{
a2 = Encoding.ASCII.GetBytes(_msg);
networkStream = clientSocket.GetStream();
networkStream.Write(a2, 0, a2.Length);
networkStream.Flush();
}
Then, each time you accept a new client, you must hold the socket for further handling (e.g. sending or receiving message) until the connection is closed. Have a look at the code below:
ArrayList arrSocket = new ArrayList(); // Declaration for holding dynamic array of socket
........
arrSocket.Add(tcpListener.AcceptSocket()); // For adding new connected client to the dynamic array
........
foreach (object obj in arrSocket) // Repeat for each connected client (socket held in a dynamic array)
{
Socket socket = (Socket)obj;
sendMessage(socket, "your message here"); // call the above sendMessage function for sending message to a client
}