Encodes and returns the L{msg<Envelope>} as an AMF stream.

    @param strict: Enforce strict encoding. Default is C{False}. Specifically
        header/body lengths will be written correctly, instead of the default 0.
        Default is `False`. Introduced in 0.4.
    @param logger: Used to log interesting events whilst decoding a remoting
        message.
    @type logger: U{logging.Logger<http://
        docs.python.org/library/logging.html#loggers>}
    @param timezone_offset: The difference between the current timezone and
        UTC. Date/times should always be handled in UTC to avoid confusion but
        this is required for legacy systems.
    @type timezone_offset: U{datetime.datetime.timedelta<http://
        docs.python.org/library/datetime.html#datetime.timedelta}
    @rtype: L{BufferedByteStream<pyamf.util.BufferedByteStream>}

def encode(msg, strict=False, logger=None, timezone_offset=None):
    """
    Encodes and returns the L{msg<Envelope>} as an AMF stream.

    @param strict: Enforce strict encoding. Default is C{False}. Specifically
        header/body lengths will be written correctly, instead of the default 0.
        Default is `False`. Introduced in 0.4.
    @param logger: Used to log interesting events whilst decoding a remoting
        message.
    @type logger: U{logging.Logger<http://
        docs.python.org/library/logging.html#loggers>}
    @param timezone_offset: The difference between the current timezone and
        UTC. Date/times should always be handled in UTC to avoid confusion but
        this is required for legacy systems.
    @type timezone_offset: U{datetime.datetime.timedelta<http://
        docs.python.org/library/datetime.html#datetime.timedelta}
    @rtype: L{BufferedByteStream<pyamf.util.BufferedByteStream>}
    """
    stream = util.BufferedByteStream()

    encoder = pyamf.get_encoder(pyamf.AMF0, stream, strict=strict,
        timezone_offset=timezone_offset)

    if msg.amfVersion == pyamf.AMF3:
        encoder.use_amf3 = True

    stream.write_ushort(msg.amfVersion)
    stream.write_ushort(len(msg.headers))

    for name, header in msg.headers.iteritems():
        _write_header(name, header, int(msg.headers.is_required(name)),
            stream, encoder, strict)

    stream.write_short(len(msg))

    for name, message in msg.iteritems():
        encoder.context.clear()

        _write_body(name, message, stream, encoder, strict)

    stream.seek(0)

    return stream