/*public static void listenAndCommunicate(IPEndPoint localEndPoint, handleConnection handler)
        {
            TcpClient client;
            byte[] buffer = new byte[Constants.READBUFFSIZE];
            MemoryStream stream = new MemoryStream(buffer);

            try
            {
                TcpListener listener = new TcpListener(localEndPoint);
                listener.Start();

                //ConnectionState state = new ConnectionState(handler, socket, buffer);
                while (true)
                {
                    TcpConnectionDone.Reset();
                    client = listener.AcceptTcpClient();
                    if (client.GetStream().CanRead)
                    {
                        client.GetStream().Read(buffer, 0, buffer.Length);

                        handler(new BinaryReader(stream), new BinaryWriter(stream));
                        //client.GetStream().BeginRead(state.buffer, 0, state.buffer.Length, SocketFlags.None,
                        //	new AsyncCallback(ReceiveCallback), state);

                        //socket.BeginReceive(state.buffer, 0, state.buffer.Length, SocketFlags.None,
                        //	new AsyncCallback(ReceiveCallback), state);
                        TcpConnectionDone.WaitOne();
                    }
                }
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }*/
        public static void listenAndCommunicateAsync(IPEndPoint localEndPoint, handleConnection handler)
        {
            Socket socket;
            byte[] buffer = new byte[Constants.READBUFFSIZE];

            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(localEndPoint.GetEndPoint());
                ConnectionState state = new ConnectionState(handler, socket, buffer);
                while (true)
                {
                    UdpConnectionDone.Reset();
                    socket.BeginReceive(state.buffer, 0, state.buffer.Length, SocketFlags.None,
                        new AsyncCallback(ReceiveCallback), state);
                    UdpConnectionDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }
 public ConnectionState(handleConnection __handler, Socket __socket, byte[] __buffer)
 {
     handler = __handler;
     socket = __socket;
     buffer = __buffer;
     position = 0;
 }
        public static void listenAndCommunicate(IPEndPoint localEndPoint, handleConnection handler)
        {
            TcpListener TCPListener = null;
            Socket newConnection = null;
            NetworkStream clientStream = null;
            BinaryReader reader = null;
            BinaryWriter writer = null;
            byte[] buffer = new byte[Constants.READBUFFSIZE];

            try
            {
                TCPListener = new TcpListener(localEndPoint.GetEndPoint());
                TCPListener.Start();

                while(true)
                {
                    newConnection = TCPListener.AcceptSocket();
                    //newConnection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive,
                    //	1);

                    if(!newConnection.Connected)
                    {
                        newConnection.Close();
                        continue;
                    }

                    //Get streams to the client so we can read and write to it.
                    clientStream = new NetworkStream(newConnection);
                    //If we can't read and write to the client abort connection
                    if(!clientStream.CanRead || !clientStream.CanWrite)
                    {
                        clientStream.Close();
                        newConnection.Close();
                        continue;
                    }
                    reader = new BinaryReader(clientStream);
                    writer = new BinaryWriter(clientStream);

                    //newConnection.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                     reader.Read(buffer, 0, buffer.Length);

                    reader = new BinaryReader(new MemoryStream(buffer));

                    //process the message
                    handler(reader, writer);

                    reader.Close();
                    writer.Close();
                    clientStream.Close();
                    newConnection.Close();
                }
            }
            catch (Exception e)
            {
                int x = 2;
            }
            finally
            {
                if(clientStream != null)
                    clientStream.Close();
                if(newConnection != null)
                    newConnection.Close();
            }
        }