def METHOD_NAME(self) -> bytes:
    if self.payload_length is None and self.data:
        self.payload_length = len(self.data)
    raw = io.BytesIO()
    raw.write(
        struct.pack(
            '!B',
            (1 << 7 if self.fin else 0) |
            (1 << 6 if self.rsv1 else 0) |
            (1 << 5 if self.rsv2 else 0) |
            (1 << 4 if self.rsv3 else 0) |
            self.opcode,
        ),
    )
    assert self.payload_length is not None
    if self.payload_length < 126:
        raw.write(
            struct.pack(
                '!B',
                (1 << 7 if self.masked else 0) | self.payload_length,
            ),
        )
    elif self.payload_length < 1 << 16:
        raw.write(
            struct.pack(
                '!BH',
                (1 << 7 if self.masked else 0) | 126,
                self.payload_length,
            ),
        )
    elif self.payload_length < 1 << 64:
        raw.write(
            struct.pack(
                '!BHQ',
                (1 << 7 if self.masked else 0) | 127,
                self.payload_length,
            ),
        )
    else:
        raise ValueError(
            f'Invalid payload_length { self.payload_length },'
            f'maximum allowed { 1 << 64 }',
        )
    if self.masked and self.data:
        mask = secrets.token_bytes(4) if self.mask is None else self.mask
        raw.write(mask)
        raw.write(self.apply_mask(self.data, mask))
    elif self.data:
        raw.write(self.data)
    return raw.getvalue()