GlowCommon.CommandServer.ServiceSocket C# (CSharp) Method

ServiceSocket() private method

Called by both the server and client to service the socket. This function will return when the socket dies.
private ServiceSocket ( StreamSocket socket ) : void
socket Windows.Networking.Sockets.StreamSocket
return void
        private void ServiceSocket(StreamSocket socket)
        {
            // Sleep a thread on the socket
            new Task(async () =>
            {
                try
                {
                    // The the listener someone connected
                    m_listener.OnConnect();

                    // Make the readers and writers
                    DataReader reader = new DataReader(socket.InputStream);
                    DataWriter writer = new DataWriter(socket.OutputStream);

                    if(m_mode == CommmandServerMode.Client)
                    {
                        // If this is the client make sure we push the writer out.
                        m_clientDataWriter = writer;
                    }

                    // Loop
                    while (true)
                    {
                        // Wait for a command
                        Command cmd = await WaitForCommand(reader);

                        // Send the command to the consumer
                        Command response = m_listener.OnCommand(cmd);

                        // If the gave a message back, send it.
                        if(response != null)
                        {
                            await InternalSendMessage(response, writer);
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("The socket listener hit an exception: " + e.Message);                    
                }

                // Call disconnect and clean up.
                m_socket = null;
                m_clientDataWriter = null;
                m_listener.OnDisconnected();

            }).Start();      
        }