/// <summary>
        /// Decodes handshake message.
        /// </summary>
        /// <param name="context">RTMP protocol state.</param>
        /// <param name="stream">Buffer to be decoded.</param>
        /// <returns>Buffer with handshake response.</returns>
        public static object DecodeHandshake(RtmpContext context, ByteBuffer stream)
        {
            long remaining = stream.Remaining;

            if (context.Mode == RtmpMode.Server)
            {
                if (context.State == RtmpState.Connect)
                {
                    if (remaining < HandshakeSize + 1)
                    {
#if !SILVERLIGHT
                        if (log.IsDebugEnabled)
                        {
                            log.Debug(__Res.GetString(__Res.Rtmp_HSInitBuffering, remaining));
                        }
#endif
                        context.SetBufferDecoding(HandshakeSize + 1);
                        return(null);
                    }
                    else
                    {
#if !SILVERLIGHT
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Handshake 1st phase");
                        }
#endif
                        stream.Get();// skip the header byte
                        byte[] handshake = RtmpHandshake.GetHandshakeResponse(stream);
                        context.SetHandshake(handshake);
                        context.State = RtmpState.Handshake;
                        return(handshake);
                    }
                }
                if (context.State == RtmpState.Handshake)
                {
                    //if (log.IsDebugEnabled)
                    //    log.Debug("Handshake reply");

                    if (remaining < HandshakeSize)
                    {
#if !SILVERLIGHT
                        if (log.IsDebugEnabled)
                        {
                            log.Debug(__Res.GetString(__Res.Rtmp_HSReplyBuffering, remaining));
                        }
#endif
                        context.SetBufferDecoding(HandshakeSize);
                        return(null);
                    }
                    else
                    {
                        // Skip first 8 bytes when comparing the handshake, they seem to be changed when connecting from a Mac client.
                        if (!context.ValidateHandshakeReply(stream, 8, HandshakeSize - 8))
                        {
#if !SILVERLIGHT
                            if (log.IsDebugEnabled)
                            {
                                log.Debug("Handshake reply validation failed, disconnecting client.");
                            }
#endif
                            stream.Skip(HandshakeSize);
                            context.State = RtmpState.Error;
                            throw new HandshakeFailedException("Handshake validation failed");
                        }
                        stream.Skip(HandshakeSize);
                        context.State = RtmpState.Connected;
                        context.ContinueDecoding();
                        return(null);
                    }
                }
            }
            else
            {
                //Client mode
                if (context.State == RtmpState.Connect)
                {
                    int size = (2 * HandshakeSize) + 1;
                    if (remaining < size)
                    {
#if !SILVERLIGHT
                        if (log.IsDebugEnabled)
                        {
                            log.Debug(__Res.GetString(__Res.Rtmp_HSInitBuffering, remaining));
                        }
#endif
                        context.SetBufferDecoding(size);
                        return(null);
                    }
                    else
                    {
                        ByteBuffer hs = ByteBuffer.Allocate(size);
                        ByteBuffer.Put(hs, stream, size);
                        hs.Flip();
                        context.State = RtmpState.Handshake;
                        return(hs);
                    }
                }
            }
            return(null);
        }
        /// <summary>
        /// Decodes handshake message.
        /// </summary>
        /// <param name="context">RTMP protocol state.</param>
        /// <param name="stream">Buffer to be decoded.</param>
        /// <returns>Buffer with handshake response.</returns>
		public static object DecodeHandshake(RtmpContext context, ByteBuffer stream) 
		{
			long remaining = stream.Remaining;
			if(context.Mode == RtmpMode.Server)
			{
				if(context.State == RtmpState.Connect)
				{
					if(remaining < HandshakeSize + 1) 
					{
#if !SILVERLIGHT
                        if( log.IsDebugEnabled )
                            log.Debug(__Res.GetString(__Res.Rtmp_HSInitBuffering, remaining));
#endif
						context.SetBufferDecoding(HandshakeSize + 1);
						return null;
					}
					else 
					{
#if !SILVERLIGHT
                        if (log.IsDebugEnabled)
                            log.Debug("Handshake 1st phase");
#endif
                        stream.Get();// skip the header byte
                        byte[] handshake = RtmpHandshake.GetHandshakeResponse(stream);
                        context.SetHandshake(handshake);
                        context.State = RtmpState.Handshake;
                        return handshake;
                    }
				}
				if(context.State == RtmpState.Handshake)
				{
                    //if (log.IsDebugEnabled)
                    //    log.Debug("Handshake reply");

					if(remaining < HandshakeSize)
					{
#if !SILVERLIGHT
                        if( log.IsDebugEnabled )
							log.Debug(__Res.GetString(__Res.Rtmp_HSReplyBuffering, remaining));
#endif
						context.SetBufferDecoding(HandshakeSize);
						return null;
					}				 
					else 
					{
					    // Skip first 8 bytes when comparing the handshake, they seem to be changed when connecting from a Mac client.
                        if (!context.ValidateHandshakeReply(stream, 8, HandshakeSize - 8))
                        {
#if !SILVERLIGHT
                            if (log.IsDebugEnabled)
                                log.Debug("Handshake reply validation failed, disconnecting client.");
#endif
                            stream.Skip(HandshakeSize);
                            context.State = RtmpState.Error;
                            throw new HandshakeFailedException("Handshake validation failed");
                        }
						stream.Skip(HandshakeSize);
						context.State = RtmpState.Connected;
						context.ContinueDecoding();
						return null;
					}
				}
			}
			else
			{
				//Client mode
				if(context.State == RtmpState.Connect)
				{
					int size = (2 * HandshakeSize) + 1;
					if(remaining < size) 
					{
#if !SILVERLIGHT
                        if( log.IsDebugEnabled )
							log.Debug(__Res.GetString(__Res.Rtmp_HSInitBuffering, remaining));
#endif
						context.SetBufferDecoding(size);
						return null;
					}
					else
					{
						ByteBuffer hs = ByteBuffer.Allocate(size);
						ByteBuffer.Put(hs, stream, size);
						hs.Flip();
						context.State = RtmpState.Handshake;
						return hs;
					}
				}
			}
			return null;
		}