title: C# Multi-threaded Server - Cross Domain Policy
body: <p>I have a server, listening for connections from a SWF file in browser.</p>
  <p>When running on local, it picks up a connection then drops it very soon after leaving in the log:</p>
  <blockquote>
  <p>[31/1/2016 18:10:14] 127.0.0.1connected.  Full 127.0.0.1:58482</p>
  <p>[31/1/2016 18:10:14] Got &lt; policy-file-request/&gt;  from client 0</p>
  <p>[31/1/2016 18:10:16] Client0 disconnected and removed.</p>
  </blockquote>
  <p>This is not logged when the server is ran and the client is launched in debug mode in flashdevelop, but the client connects and acts as desired.</p>
  <p>I am including my TcpClient class, have i made a mistake or typo or should this be behaving differently when ran in browser and not in flashdevelop?</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  
  namespace ParticleFramework.Communication
  {
      class TcpClient
      {
          #region Required Variables
          public Socket socket;
          public int index;
          private byte[] dataBuffer = new byte[0x400];
          private AsyncCallback ReceiveCallback;
          private AsyncCallback SendCallback;
          #endregion
  
          #region ArchiCruise Vars
          public ArchiCruise.Users.UserObject userObject;
          public string ip;
          #endregion
  
          public TcpClient(Socket sock, int num)
          {
              index = num;
              socket = sock;
  
              ip = socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0];
  
              ReceiveCallback = new AsyncCallback(this.ReceivedData);
              SendCallback = new AsyncCallback(this.sentData);
  
              this.WaitForData();
          }
  
          public void Disconnect()
          {
              if (socket.Connected)
              {
                  socket.Close();
                  if (userObject != null) userObject.remove();
                  Particle.Server.removeClient(this);
                  Log.Info(&quot;Client&quot; + this.index + &quot; disconnected and removed.&quot;);
                  Console.WriteLine(&quot;Client&quot; + this.index + &quot; disconnected.&quot;);
              }
          }
  
          private void ReceivedData(IAsyncResult iAr)
          {
              try
              {
                  int count = 0;
  
                  try
                  {
                      count = socket.EndReceive(iAr);
                  }
                  catch
                  {
                      Disconnect();
                  }
  
                  StringBuilder builder = new StringBuilder();
                  builder.Append(System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count));
                  string str = System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count);
  
                  if (str.Contains(&quot;&lt;policy-file-requet/&gt;&quot;))
                  {
                      Log.Info(&quot;Sending policy file to client&quot; + this.index);
                      rawSend(&quot;&lt;?xml version\&quot;1.0\&quot;?&gt;&lt;cross-domain-policy&gt;&lt;allow-access-from-domain=\&quot;*\&quot; to-ports=\&quot;*\&quot; /&gt;&lt;cross-domain-policy&gt;&quot; + Convert.ToChar(0));
                  }
                  else if (!(str.ToString() == &quot;&quot;))
                  {
                      string packet = str.Substring(0, str.Length - 1);
                      //packet = ArchiCruise.Security.Encryption.decrypt(packet);
                      Log.Info(&quot;Got &quot; + str + &quot; from client &quot; + this.index);
  
                      Particle.packetClass.handle(packet, this);
                  }
                  else
                  {
                      Disconnect();
                  }
              }
              catch (Exception exception)
              {
                  Log.Info(&quot;Data recieve error: &quot; + exception.ToString() + &quot; &quot; + exception.Source);
                  Disconnect();
              }
              finally
              {
                  this.WaitForData();
              }
          }
  
          private void WaitForData()
          {
              try
              {
                  socket.BeginReceive(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, this.ReceiveCallback, socket);
              }
              catch
              {
                  Disconnect();
              }
          }
  
          public void sendData(string Data)
          {
              Data += (char)1;
              rawSend(Data);
          }
  
          internal void rawSend(string Data)
          {
              try
              {
                  Data += &quot;\0&quot;;
                  byte[] bytes = System.Text.Encoding.Default.GetBytes(Data);
  
                  socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(this.sentData), null);
                  Log.Info(&quot;Sent &quot; + Data + &quot; to client &quot; + this.index);
              }
              catch
              {
                  Disconnect();
              }
          }
  
          private void sentData(IAsyncResult iAr)
          {
              try
              {
                  socket.EndSend(iAr);
              }
              catch
              {
                  Disconnect();
              }
          }
      }
  }
  </code></pre>