/// <summary>Used for debugging on server.</summary>
 public bool printOnServer()
 {
     try {
         string toSend = "PRINT";
         socket.getOut().WriteLine(toSend);
         socket.getOut().Flush();
         return(true);
     } catch (Exception e) {
         ChessActivity.OnServerFail();
         return(false);
     }
 }
 /// <summary>Posts a message to chat room.</summary>
 public bool postToChat(string message)
 {
     try {
         string toSend = message;
         socket.getOut().WriteLine(toSend);
         socket.getOut().Flush();
         return(true);
     } catch (Exception e) {
         ChessActivity.OnServerFail();
         return(false);
     }
 }
 /// <summary>Returns string of all last chat messages.</summary>
 public string getLastChat()
 {
     try {
         string toSend = "GET";
         socket.getOut().WriteLine(toSend);
         socket.getOut().Flush();
         // then recieve from socket
         return("");
     } catch (Exception e) {
         ChessActivity.OnServerFail();
         return("");
     }
 }
        /// <summary>Log in to the server.</summary>
        public bool login()
        {
            try {
                string toSend = "LOGIN " + username;
                socket.getOut().WriteLine(toSend);
                socket.getOut().Flush();

                return(true);
            } catch (Exception e) {
                ChessActivity.OnServerFail();
                return(false);
            }
        }
 /// <summary>Joins an existing game. (you will be player 2)</summary>
 public bool joinGame()
 {
     try {
         string toSend = "JOIN " + opponent;
         socket.getOut().WriteLine(toSend);
         socket.getOut().Flush();
         endGame = false;
         string recieve = socket.getIn().ReadLine();
         if (recieve == "TRUE")
         {
             return(true);
         }
         return(false);
     } catch (Exception e) {
         ChessActivity.OnServerFail();
         return(false);
     }
 }
        /// <summary>Get last move made by other player,
        /// false is returned if no move yet made.</summary>
        public bool getLastMove(gameSquare[,] chessBoard)
        {
            try {
                string toSend = "GETMOVE";
                socket.getOut().WriteLine(toSend);
                socket.getOut().Flush();
                string recieve;
                recieve = socket.getIn().ReadLine();
                string[] recieveSplit;
                recieveSplit = recieve.Split(' ');
                if (recieveSplit [0] == "MOVE")
                {
                    int x1 = Convert.ToInt32(recieveSplit [1]);
                    int y1 = Convert.ToInt32(recieveSplit [2]);
                    int x2 = Convert.ToInt32(recieveSplit [3]);
                    int y2 = Convert.ToInt32(recieveSplit [4]);
                    // the move made by player 2
                    if (chessBoard[x2, y2].piece == chessman.King)
                    {
                        endGame = true;
                    }

                    if (chessBoard[x1, y1].piece == chessman.Pawn && (x2 == 0 || x2 == 7))
                    {
                        chessBoard[x1, y1].piece = chessman.Queen;
                    }

                    chessBoard [x2, y2] = chessBoard [x1, y1];
                    chessBoard [x1, y1] = new gameSquare {
                        colour = chessmanColour.empty, piece = chessman.empty
                    };
                    return(true);
                }
                else
                {
                    return(false);
                }
            } catch (Exception e) {
                ChessActivity.OnServerFail();
                return(false);
            }
        }
        /// <summary>Moves a game piece.</summary>
        /// <remarks><para>(x1,y1) - from postion, (x2,y2) - to position</para></remarks>

        public bool move(gameSquare[,] chessBoard, int x1, int y1, int x2, int y2)
        {
            try {
                // check parameters are legal
                if (x1 >= 0 && x1 <= 7 &&
                    x2 >= 0 && x2 <= 7 &&
                    y1 >= 0 && y1 <= 7 &&
                    y2 >= 0 && y2 <= 7)
                {
                    // make sure it's your piece to move
                    if ((chessBoard [x1, y1].colour == chessmanColour.white && isWhite) || chessBoard [x1, y1].colour == chessmanColour.black && !isWhite)
                    {
                        // check if the move is legal

                        if ((chessBoard [x1, y1].colour == chessmanColour.white && chessBoard [x2, y2].colour == chessmanColour.white) ||
                            (chessBoard [x1, y1].colour == chessmanColour.black && chessBoard [x2, y2].colour == chessmanColour.black))
                        {
                            return(false);
                        }

                        bool isLegal = false;
                        switch (chessBoard [x1, y1].piece)
                        {
                        case chessman.Bishop:
                            isLegal = bishopLegal(chessBoard, x1, y1, x2, y2);
                            break;

                        case chessman.King:
                            isLegal = kingLegal(chessBoard, x1, y1, x2, y2);
                            break;

                        case chessman.Knight:
                            isLegal = knightLegal(chessBoard, x1, y1, x2, y2);
                            break;

                        case chessman.Pawn:
                            isLegal = pawnLegal(chessBoard, x1, y1, x2, y2);
                            if (isLegal && (x2 == 7 || x2 == 0))
                            {
                                chessBoard [x1, y1].piece = chessman.Queen;
                            }
                            break;

                        case chessman.Queen:
                            isLegal = queenLegal(chessBoard, x1, y1, x2, y2);
                            break;

                        case chessman.Rook:
                            isLegal = rookLegal(chessBoard, x1, y1, x2, y2);
                            break;
                        }

                        /*	Android.Widget.Toast.MakeText (Android.App.Application.Context, isLegal.ToString (),
                         *              Android.Widget.ToastLength.Long).Show ();
                         */
                        if (isLegal)
                        {
                            string toSend = "MOVE " + x1.ToString() + " " + y1.ToString() + " " + x2.ToString() + " " + y2.ToString();
                            if (chessBoard[x2, y2].piece == chessman.King)
                            {
                                endGame = true;
                            }
                            chessBoard [x2, y2] = chessBoard [x1, y1];
                            chessBoard [x1, y1] = new gameSquare {
                                colour = chessmanColour.empty, piece = chessman.empty
                            };
                            socket.getOut().WriteLine(toSend);
                            socket.getOut().Flush();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            } catch (Exception e) {
                ChessActivity.OnServerFail();
                return(false);
            }
        }