I have a simple application that can receive and send tcp data.right now there's a thread used to start the listener and within this thread there's another thread to receive.it works but it seems like a waste of resources. Can the connect be simplified?    public partial class Form1 : Form    {    private readonly TcpListener server = new TcpListener(IPAddress.Any, 1980);    private NetworkStream sendStream;    private NetworkStream recvStream;    private TcpClient client;    private byte[] datalength = new byte[4];    public Form1()    {        InitializeComponent();    }    public void ServerReceive()    {        recvStream = client.GetStream();        new Thread(() =>        {            while (recvStream.Read(datalength, 0, 4) != 0)            {                byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];                recvStream.Read(data, 0, data.Length);                this.Invoke((MethodInvoker)delegate                {                    txtLog.Text += System.Environment.NewLine + Client :  + Encoding.Default.GetString(data);                });            }        }).Start();    }    public void ServerSend(string msg)    {        sendStream = client.GetStream();        var data = Encoding.Default.GetBytes(msg);        var length = data.Length;        datalength = BitConverter.GetBytes(length);        sendStream.Write(datalength, 0, 4);        sendStream.Write(data, 0, data.Length);    }    private void btnListen_Click(object sender, EventArgs e)    {        Connect();    }    private void btnSend_Click(object sender, EventArgs e)    {        if (client.Connected)        {            ServerSend(txtSend.Text);        }    }    private void Form1_Load(object sender, EventArgs e)    {        Connect();    }    private void Connect()    {        server.Start();        MessageBox.Show(Waiting For Connection);        new Thread(() =>        {            client = server.AcceptTcpClient();            MessageBox.Show(Connected To Client);            if (client.Connected)            {                ServerReceive();            }        }).Start();    }}
Simplify TcpListener application in C#
c#;multithreading;tcp