      |K|             |
      +-+-------------+
      */
-    func serializeMaskAndLength() -> [Byte] {
-        var bytes: [Byte] = []
-
-        var leadingByte: Byte = 0
+    private func serializeMaskAndLength() -> (byte: Byte, extended: ExtendedPayloadByteLength?) {
+        var byte: Byte = 0
         if isMasked {
-            leadingByte |= Byte.maskKeyIncluded
+            byte |= Byte.maskKeyIncluded
         }
 
         // 126 / 127 (max, max-1) indicate 2 & 8 byte extensions respectively
         if payloadLength < 126 {
-            leadingByte |= UInt8(payloadLength)
-            return [leadingByte]
+            byte |= UInt8(payloadLength)
+            return (byte, nil)
         } else if payloadLength < UInt16.max.toUIntMax() {
-            leadingByte |= 126 // 126 flags that 2 bytes are required
+            byte |= 126 // 126 flags that 2 bytes are required
+            return (byte, .two)
         } else {
-            leadingByte |= 127 // 127 flags that 8 bytes are requred
-        }
-
-        return bytes
+            byte |= 127 // 127 flags that 8 bytes are requred
+            return (byte, .eight)
+        }
     }
 }
 

//    return str
//}

public final class MessageSerializer {
    private let message: WebSocketMessage
    private init(_ message: WebSocketMessage) {
        self.message = message
    }
}


    case key(zero: UInt8, one: UInt8, two: UInt8, three: UInt8)
}

extension MaskingKey {
    func serialize() -> [Byte] {
        switch self {
        case .none:
            return []
        case let .key(zero: zero, one: one, two: two, three: three):
            return [zero, one, two, three]
        }
    }
}

public struct WebSocketHeader {
    let fin: Bool

