/// <summary>
        /// Empezar el handshake con el server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool doHandShake()
        {
            try
            {
                int    serverHandshakeLength = Encoding.ASCII.GetBytes(TgcSocketServer.SERVER_HANDSHAKE).Length;
                byte[] data = new byte[serverHandshakeLength];
                int    recv = clientSocket.Receive(data, data.Length, SocketFlags.None);
                if (recv > 0 && recv == serverHandshakeLength)
                {
                    string msg = Encoding.ASCII.GetString(data, 0, recv);
                    if (msg.Equals(TgcSocketServer.SERVER_HANDSHAKE))
                    {
                        //Server correcto, enviar informacion inicial
                        status = TgcSocketClientInfo.ClientStatus.RequireInitialInfo;
                        TgcSocketInitialInfoClient clientInitInfo = new TgcSocketInitialInfoClient();
                        clientInitInfo.clientName = clientName;

                        TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                        sendMsg.write(clientInitInfo);
                        TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);

                        return(true);
                    }
                }
            }
            catch (SocketException)
            {
                //Handshake incorrecto
                return(false);
            }

            return(false);
        }
        /// <summary>
        /// Recibir informacion inicial del server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool getServerInitialInfo()
        {
            try
            {
                //Recibir info inicial del server
                TgcSocketRecvMsg msg = TgcSocketMessages.receiveMessage(clientSocket, TgcSocketMessageHeader.MsgType.InitialMessage);
                if (msg == null)
                {
                    return(false);
                }

                //Guardar sus datos y cambiar su estado
                TgcSocketInitialInfoServer serverInitInfo = (TgcSocketInitialInfoServer)msg.readNext();
                serverInfo.Name = serverInitInfo.serverName;
                playerId        = serverInitInfo.playerId;

                //Enviar OK final
                this.status = TgcSocketClientInfo.ClientStatus.Connected;
                TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                sendMsg.write(true);
                return(TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage));
            }
            catch (SocketException)
            {
                return(false);
            }
        }
 /// <summary>
 /// Desconecta el cliente
 /// </summary>
 public void disconnectClient()
 {
     if (clientSocket != null)
     {
         try
         {
             clientSocket.Shutdown(SocketShutdown.Both);
             clientSocket.Close();
         }
         catch (Exception)
         {
         }
         status       = TgcSocketClientInfo.ClientStatus.Disconnected;
         clientSocket = null;
     }
 }
        /// <summary>
        /// Se conecta a un nuevo servidor. Pero todavía no lo toma como definitivo hasta
        /// que no se hace la selección final.
        /// </summary>
        /// <param name="ip">IP del server</param>
        /// <param name="port">Puerto del server</param>
        /// <returns>True si todo salio bien</returns>
        public bool connect(string ip, int port)
        {
            try
            {
                //Conectar con el server, en forma no bloqueante
                IPAddress  address = IPAddress.Parse(ip);
                IPEndPoint Ipep    = new IPEndPoint(address, port);
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(Ipep);
                clientSocket.Blocking = false;

                serverInfo = new TgcSocketServerInfo(address, port);

                //Enviar mensaje de Handshake
                clientSocket.Send(Encoding.ASCII.GetBytes(CLIENT_HANDSHAKE));
                status = TgcSocketClientInfo.ClientStatus.HandshakePending;

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public void initializeClient(string clientName)
 {
     this.clientName = clientName;
     this.receivedMessages.Clear();
     this.status = TgcSocketClientInfo.ClientStatus.Disconnected;
 }
        /// <summary>
        /// Se conecta a un nuevo servidor. Pero todavía no lo toma como definitivo hasta
        /// que no se hace la selección final.
        /// </summary>
        /// <param name="ip">IP del server</param>
        /// <param name="port">Puerto del server</param>
        /// <returns>True si todo salio bien</returns>
        public bool connect(string ip, int port)
        {
            try
            {
                //Conectar con el server, en forma no bloqueante
                IPAddress address = IPAddress.Parse(ip);
                IPEndPoint Ipep = new IPEndPoint(address, port);
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(Ipep);
                clientSocket.Blocking = false;

                serverInfo = new TgcSocketServerInfo(address, port);

                //Enviar mensaje de Handshake
                clientSocket.Send(Encoding.ASCII.GetBytes(CLIENT_HANDSHAKE));
                status = TgcSocketClientInfo.ClientStatus.HandshakePending;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// Recibir informacion inicial del server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool getServerInitialInfo()
        {
            try
            {
                //Recibir info inicial del server
                TgcSocketRecvMsg msg = TgcSocketMessages.receiveMessage(clientSocket, TgcSocketMessageHeader.MsgType.InitialMessage);
                if (msg == null)
                {
                    return false;
                }

                //Guardar sus datos y cambiar su estado
                TgcSocketInitialInfoServer serverInitInfo = (TgcSocketInitialInfoServer)msg.readNext();
                serverInfo.Name = serverInitInfo.serverName;
                playerId = serverInitInfo.playerId;

                //Enviar OK final
                this.status = TgcSocketClientInfo.ClientStatus.Connected;
                TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                sendMsg.write(true);
                return TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);
            }
            catch (SocketException)
            {
                return false;
            }
        }
        /// <summary>
        /// Empezar el handshake con el server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool doHandShake()
        {
            try
            {
                int serverHandshakeLength = Encoding.ASCII.GetBytes(TgcSocketServer.SERVER_HANDSHAKE).Length;
                byte[] data = new byte[serverHandshakeLength];
                int recv = clientSocket.Receive(data, data.Length, SocketFlags.None);
                if (recv > 0 && recv == serverHandshakeLength)
                {
                    string msg = Encoding.ASCII.GetString(data, 0, recv);
                    if (msg.Equals(TgcSocketServer.SERVER_HANDSHAKE))
                    {
                        //Server correcto, enviar informacion inicial
                        status = TgcSocketClientInfo.ClientStatus.RequireInitialInfo;
                        TgcSocketInitialInfoClient clientInitInfo = new TgcSocketInitialInfoClient();
                        clientInitInfo.clientName = clientName;

                        TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                        sendMsg.write(clientInitInfo);
                        TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);

                        return true;
                    }
                }
            }
            catch (SocketException)
            {
                //Handshake incorrecto
                return false;
            }

            return false;
        }
 public void initializeClient(string clientName)
 {
     this.clientName = clientName;
     this.receivedMessages.Clear();
     this.status = TgcSocketClientInfo.ClientStatus.Disconnected;
 }
 /// <summary>
 /// Desconecta el cliente
 /// </summary>
 public void disconnectClient()
 {
     if (clientSocket != null)
     {
         try
         {
             clientSocket.Shutdown(SocketShutdown.Both);
             clientSocket.Close();
         }
         catch (Exception)
         {
         }
         status = TgcSocketClientInfo.ClientStatus.Disconnected;
         clientSocket = null;
     }
 }