/// <summary>
        /// Serializes a message to a byte array to send to remote application.
        ///             This method is synchronized. So, only one thread can call it concurrently.
        ///
        /// </summary>
        /// <param name="message">Message to be serialized</param><exception cref="T:Tera.NetworkApi.Communication.Scs.Communication.CommunicationException">Throws CommunicationException if message is bigger than maximum allowed message length.</exception>
        public byte[] GetBytes(IScsMessage message)
        {
            byte[] numArray = this.SerializeMessage(message);
            int    length   = numArray.Length;

            if (length > 134217728)
            {
                throw new CommunicationException("Message is too big (" + (object)length + " bytes). Max allowed length is " + (string)(object)134217728 + " bytes.");
            }
            else
            {
                byte[] buffer = new byte[length + 4];
                BinarySerializationProtocol.WriteInt32(buffer, 0, length);
                Array.Copy((Array)numArray, 0, (Array)buffer, 4, length);
                return(buffer);
            }
        }
        /// <summary>
        /// This method tries to read a single message and add to the messages collection.
        ///
        /// </summary>
        /// <param name="messages">Messages collection to collect messages</param>
        /// <returns>
        /// Returns a boolean value indicates that if there is a need to re-call this method.
        ///
        /// </returns>
        /// <exception cref="T:Tera.NetworkApi.Communication.Scs.Communication.CommunicationException">Throws CommunicationException if message is bigger than maximum allowed message length.</exception>
        private bool ReadSingleMessage(ICollection <IScsMessage> messages)
        {
            this._receiveMemoryStream.Position = 0L;
            if (this._receiveMemoryStream.Length < 4L)
            {
                return(false);
            }
            int length = BinarySerializationProtocol.ReadInt32((Stream)this._receiveMemoryStream);

            if (length > 134217728)
            {
                throw new Exception("Message is too big (" + (object)length + " bytes). Max allowed length is " + (string)(object)134217728 + " bytes.");
            }
            else if (length == 0)
            {
                if (this._receiveMemoryStream.Length == 4L)
                {
                    this._receiveMemoryStream = new MemoryStream();
                    return(false);
                }
                else
                {
                    byte[] buffer = this._receiveMemoryStream.ToArray();
                    this._receiveMemoryStream = new MemoryStream();
                    this._receiveMemoryStream.Write(buffer, 4, buffer.Length - 4);
                    return(true);
                }
            }
            else if (this._receiveMemoryStream.Length < (long)(4 + length))
            {
                this._receiveMemoryStream.Position = this._receiveMemoryStream.Length;
                return(false);
            }
            else
            {
                byte[] bytes = BinarySerializationProtocol.ReadByteArray((Stream)this._receiveMemoryStream, length);
                messages.Add(this.DeserializeMessage(bytes));
                byte[] buffer = BinarySerializationProtocol.ReadByteArray((Stream)this._receiveMemoryStream, (int)(this._receiveMemoryStream.Length - (long)(4 + length)));
                this._receiveMemoryStream = new MemoryStream();
                this._receiveMemoryStream.Write(buffer, 0, buffer.Length);
                return(buffer.Length > 4);
            }
        }
 /// <summary>
 /// Deserializes and returns a serialized integer.
 ///
 /// </summary>
 ///
 /// <returns>
 /// Deserialized integer
 /// </returns>
 private static int ReadInt32(Stream stream)
 {
     byte[] numArray = BinarySerializationProtocol.ReadByteArray(stream, 4);
     return((int)numArray[0] << 24 | (int)numArray[1] << 16 | (int)numArray[2] << 8 | (int)numArray[3]);
 }