def encode_message(chat_name, port, message_type, sender_id, message, length):
    """Encode message for sending. In the format of ChatName_ListenPort_type_id_content_length.
    Actually length is only useful when sending images or files.
    """
    message = '{}_{}_{}_{}_{}_{}'.format(
        chat_name, port, message_type, sender_id, message, length)
    return message.encode()