private void HandleCmd(Socket client, String cmd)
        {
            try
            {
                SocketMessage sm = new SocketMessage();
                sm.Client         = clients[client];
                sm.Time           = DateTime.Now;
                sm.isLoginMessage = false;
                if (cmd.StartsWith("<NewGame>"))
                {
                    Console.WriteLine("<NewGame>" + gameId);
                    int size = Convert.ToInt32(cmd.Substring(9));
                    lock (lockGameId)
                    {
                        sm.Message             = "#NewGame" + gameId + "";
                        clients[client].roomId = gameId;
                        GameRoom room = new GameRoom(clients[client], gameId, size);
                        this.rooms.Add(gameId, room);
                        gameId++;
                    }
                    clients[client].IsCreater = true;

                    byte[] msgs = PackageServerData(sm);
                    client.Send(msgs, msgs.Length, SocketFlags.None);
                }
                else if (cmd.StartsWith("<EnterGame>"))
                {
                    //<EnterGame>1
                    String roomId = cmd.Substring(11);
                    Console.WriteLine("<EnterGame>" + roomId);
                    Console.WriteLine(roomId);
                    if (rooms.ContainsKey(Convert.ToInt32(roomId)) && rooms[Convert.ToInt32(roomId)].enemy == null)
                    {
                        rooms[Convert.ToInt32(roomId)].enemy = clients[client];
                        sm.Message             = "#GameStart" + rooms[Convert.ToInt32(roomId)].size;
                        clients[client].roomId = Convert.ToInt32(roomId);
                        byte[] msgs = PackageServerData(sm);
                        client.Send(msgs, msgs.Length, SocketFlags.None);
                        rooms[Convert.ToInt32(roomId)].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                        clients[client].IsCreater = false;
                    }
                    else
                    {
                        Console.WriteLine("rooms.ContainsKey(Convert.ToInt32(roomId):" + rooms.ContainsKey(Convert.ToInt32(roomId)));

                        String msg  = "#Msg" + "InvalidRoomId";
                        byte[] msgs = PackageServerData(msg);
                        client.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<Place>"))
                {
                    Console.WriteLine("<Place>");
                    String pix = cmd.Substring(7);
                    sm.Message = "#Place" + pix;
                    byte[] msgs = PackageServerData(sm);
                    if (clients[client].IsCreater)
                    {
                        rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                    else
                    {
                        rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<EndGame>"))
                {
                    Console.WriteLine("<EndGame>");
                    String pix = cmd.Substring(7);
                    sm.Message = "#EndGame" + pix;
                    byte[] msgs = PackageServerData(sm);
                    rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                }
                else if (cmd.StartsWith("<Undo>"))
                {
                    Console.WriteLine("<Undo>");
                    String pix = cmd.Substring(6);
                    sm.Message = "#Undo" + pix;
                    byte[] msgs = PackageServerData(sm);
                    if (clients[client].IsCreater)
                    {
                        rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                    else
                    {
                        rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<Surrender>"))
                {
                    Console.WriteLine("<Surrender>");
                    sm.Message = "#Surrender";
                    byte[] msgs = PackageServerData(sm);
                    if (clients[client].IsCreater)
                    {
                        rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                    else
                    {
                        rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<ReStart>"))
                {
                    Console.WriteLine("<ReStart>");
                    sm.Message = "#ReStart";
                    byte[] msgs = PackageServerData(sm);
                    if (clients[client].IsCreater)
                    {
                        rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                    else
                    {
                        rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<Msg>"))
                {
                    Console.WriteLine("<Msg>");
                    String msg = cmd.Substring(5);
                    sm.Message = "#Msg" + msg;
                    byte[] msgs = PackageServerData(sm);
                    if (clients[client].IsCreater)
                    {
                        rooms[clients[client].roomId].enemy.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                    else
                    {
                        rooms[clients[client].roomId].creater.socket.Send(msgs, msgs.Length, SocketFlags.None);
                    }
                }
                else if (cmd.StartsWith("<Bye>"))
                {
                    Console.WriteLine("<Bye>");
                    disconnect(client);
                }
                else if (cmd.StartsWith("<RandomRoom>"))
                {
                    Console.WriteLine("<RandomRoom>");
                    String msg = "#RandomRoom";
                    lock (lockWaitingQueue)
                    {
                        if (waitingQueue.Count > 0)
                        {
                            clients[client].IsCreater = false;
                            Socket creater = waitingQueue.Dequeue() as Socket;
                            clients[client].roomId = clients[creater].roomId;
                            rooms[clients[creater].roomId].enemy = clients[client];
                            byte[] msgsRed = PackageServerData(msg + "Red");
                            creater.Send(msgsRed, msgsRed.Length, SocketFlags.None);
                            byte[] msgsBlue = PackageServerData(msg + "Blue");
                            client.Send(msgsBlue, msgsBlue.Length, SocketFlags.None);
                        }
                        else
                        {
                            clients[client].IsCreater = true;
                            lock (lockGameId)
                            {
                                clients[client].roomId = gameId;
                                GameRoom room = new GameRoom(clients[client], gameId, 12);
                                this.rooms.Add(gameId, room);
                                gameId++;
                            }
                            waitingQueue.Enqueue(client);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        private void Recieve(IAsyncResult result)
        {
            Socket client = result.AsyncState as Socket;

            if (client == null || !clients.ContainsKey(client))
            {
                return;
            }
            try
            {
                if (!SocketConnected(client))
                {
                    disconnect(client);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Recieve Error:" + ex);
            }

            try
            {
                int    length = client.EndReceive(result);
                byte[] buffer = clients[client].buffer;

                //接收消息
                client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), client);
                string msg = Encoding.UTF8.GetString(buffer, 0, length);

                if (!clients[client].IsHandShaked && msg.Contains("Sec-WebSocket-Key"))
                {
                    client.Send(PackageHandShakeData(buffer, length));
                    clients[client].IsHandShaked = true;
                    return;
                }

                msg = AnalyzeClientData(buffer, length);

                SocketMessage sm = new SocketMessage();
                sm.Client = clients[client];
                sm.Time   = DateTime.Now;

                Regex reg = new Regex(@"{<(.*?)>}");
                Match m   = reg.Match(msg);
                if (m.Value != "")
                { //处理客户端传来的用户名
                    clients[client].NickName = Regex.Replace(m.Value, @"{<(.*?)>}", "$1");
                    sm.isLoginMessage        = true;
                    sm.Message = "login!";
                    Console.WriteLine("{0} login @{1}", client.RemoteEndPoint, DateTime.Now);
                }
                else
                { //处理客户端传来的普通消息
                    HandleCmd(client, msg);
                    sm.isLoginMessage = false;
                    sm.Message        = msg;
                    Console.WriteLine("{0} @ {1}\r\t{2}", clients[client].Name, DateTime.Now, sm.Message);
                }
                msgPool.Add(sm);
            }
            catch (Exception ex)
            {
                try
                {
                    disconnect(client);
                }
                catch (Exception exx)
                {
                    Console.WriteLine(exx);
                }
            }
        }