public void ProcessMessage(IClient client, InboundMessage message)
        {
            string text = message.StringMessage;

            if (client.IsInChatroom)
            {
                if (text.ToLower().Equals("/leave"))
                {
                    ChatRoom chatRoom = this.Chatrooms.FirstOrDefault(cr => cr.UniqueId == client.ChatRoomUniqueId);
                    if (chatRoom != null)
                    {
                        chatRoom.ClientLeave(client);
                    }
                }
                else
                {//chatroom broadcast - chat area
                    ChatRoom chatRoom = this.Chatrooms.FirstOrDefault(cr => cr.UniqueId == client.ChatRoomUniqueId);
                    if (chatRoom != null)
                    {
                        chatRoom.BroadCastMessage(new OutboundMessage(client.Nickname + ": " + text));
                        return;//input prompt is dealt with above
                    }
                }
            }
            else if (!client.HasNickname)
            {
                if (this.Clients.Any(c => c.Nickname == text))
                {
                    client.Send(new OutboundMessage("Sorry, name taken."));
                    PromptForNickname(client);
                    return;
                }

                client.SetNickname(text);
                client.Send(new OutboundMessage("Welcome " + text + "!"));
                client.Send(new OutboundMessage("To see available chatrooms type: /rooms"));
            }
            else if (text.ToLower() == "/rooms")
            {
                SendActiveRoomsTo(client);
            }
            else if (text.ToLower().StartsWith("/join "))
            {
                string   roomName        = text.ToLower().Replace("/join ", "");
                ChatRoom chatroomToEnter = this.Chatrooms.FirstOrDefault(cr => cr.Name.Equals(roomName));
                if (chatroomToEnter != null)
                {
                    client.Send(new OutboundMessage("entering room: " + chatroomToEnter.Name));
                    chatroomToEnter.ClientJoin(client);
                    client.EnterChatroom(chatroomToEnter.UniqueId);

                    foreach (IClient chatroomClient in chatroomToEnter.Clients)
                    {
                        string clientNameStr =
                            client.Equals(chatroomClient)
                            ? "* " + chatroomClient.Nickname + "(** this is you)"
                            : "* " + chatroomClient.Nickname;
                        client.Send(new OutboundMessage(clientNameStr));
                    }

                    client.Send(new OutboundMessage("end of list."));
                    client.Send(new OutboundMessage("To leave type: /leave"));
                }
                else
                {
                    client.Send(new OutboundMessage("Sorry, no such room."));
                    this.SendActiveRoomsTo(client);
                }
            }
            else
            {
                client.Send(new OutboundMessage("Invalid command, join a chatroom to start chatting!"));

                this.SendActiveRoomsTo(client);
            }

            SendClientInputPrompt(client);
        }
        private async void Listen(WebClient client)
        {
            WebSocket webSocket = client.Socket;

            try
            {
                byte[] byteBuffer = new byte[BufferSize];
                ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(byteBuffer);

                if (webSocket.State == WebSocketState.Open)
                {
                    WebSocketReceiveResult receiveResult;
                    try
                    {
                        //CancellationTokenSource ct = new CancellationTokenSource(10000);
                        //CancellationToken token = ct.Token;
                        receiveResult = await webSocket.ReceiveAsync(receiveBuffer, CancellationToken.None);
                    }
                    catch (Exception)
                    {
                        this.Listen(client);
                        return;
                    }

                    if (!receiveBuffer.Any())
                    {
                        this.Listen(client);
                    }

                    client.AppendBytes(receiveBuffer.Array.Where(b => !b.Equals((byte)0)).ToArray());

                    if (receiveResult.EndOfMessage == false)
                    {
                        this.Listen(client);
                        return;
                    }

                    InboundMessage message = new InboundMessage(client.CurrentBytesSentWithoutNewLine.ToArray());

                    client.ClearBytes();

                    Console.WriteLine("WEB Text: " + message.StringMessage);

                    if (message.StringMessage == "/quit") // Client wants to exit gracefully
                    {
                        client.Send(new OutboundMessage("BYE"));
                        CloseClientConnection(client);
                        return;
                    }

                    this.ClientService.ProcessMessage(client, message);

                    this.Listen(client);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);

                this.CloseClientConnection(client);

                if (webSocket != null)
                {
                    webSocket.Dispose();
                }
            }
        }