@Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {

        // Will use the first 23 bytes to detect the policy file request.
        if (buffer.readableBytes() >= 23) {
            ChannelPipeline p = ctx.getPipeline();
            ChannelBuffer firstMessage = buffer.readBytes(23);

            if (FLASH_POLICY_REQUEST.equals(firstMessage)) {
                p.addAfter("flashpolicydecoder", "flashpolicyhandler",
                        new FlashPolicyFileHandler(executor, exceptionHandler, ioExceptionHandler, this.publicPort));
            }

            p.remove(this);

            if (buffer.readable()) {
                return new Object[]{firstMessage, buffer.readBytes(buffer.readableBytes())};
            } else {
                return firstMessage;
            }

        }

        // Forward the current buffer as is to handlers.
        return buffer.readBytes(buffer.readableBytes());

    }