<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _gen_packet(self, sequence, payloadtype, payload=None):
 """ Generate packet header. """

        contents = self._gen_header(sequence, payloadtype)

        # add payload
        if payload:
            contents.extend(payload)

        # get packet size
        size = pack("<H", len(contents) + 2)

        # assemble complete packet
        packet = bytearray(size)
        packet.extend(contents)

        return packet