title: C# Socket Client: Understand and Manage messages
body: <p>I need to implement a Socket Client in C#.</p>
  
  <p>The socket server is a software connected with my C# client through the port 3000.</p>
  
  <p>Every message is composed as follow:</p>
  
  <ol>
  <li>Some fields: 4 bytes</li>
  <li>Message Length: 2 bytes</li>
  <li>Some fields: 4 bytes</li>
  <li>Message Index: 2 bytes</li>
  <li>Some fields: depends on the 'Message Length' field</li>
  </ol>
  
  <p>When the client Receive, the buffer could contain more the one message and repeated messages.</p>
  
  <p>I have to split the content of the buffer in message, save the message in a list if that message is not already present in this list.</p>
  
  <p>I understand that the message is already present in the list if the index of the message is in the list.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Net;
  using System.Net.Sockets;
  
  namespace Client
  {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  
      IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
  
  
      private void btnC_Click(object sender, EventArgs e)
      {
  
          sck.Connect(endPoint);
  
          if (sck.Connected)
          {
              Form1.ActiveForm.Text = Form1.ActiveForm.Text + " - Connected";
          }
  
          byte[] buffer = new byte[255];
          int rec;
  
          while (true)
          {
              buffer = new byte[255];
              rec = sck.Receive(buffer, 0, buffer.Length, 0);
              Array.Resize(ref buffer, rec);
  
  
              /* Understand and Manage the messages */
  
  
          }
      }
  }
   }
  </code></pre>
  
  <p>Have you some suggestion to implement the correct code to understand and manage the received messages ??</p>
  
  <p>Thanks in advance!</p>
  
  <p>Fabio</p>