NWProtocolWebSocket: How to get the HTTP error?

I've managed to put together a WebSocket client in Swift using NWProtocolWebSocket (though the documentation does not make it easy.)

The point I'm stuck on is how to get a meaningful error if the server rejects the HTTP request, for example with a 404 or 403 status. The error reported to my stateUpdateHandler is a low-level POSIXErrorCode(rawValue: 53): Software caused connection abort).

Additionally, how can I add custom headers to the HTTP request, like authorization or cookies?

(I'm kind of wondering whether good ol' NSURLSession would have been a better choice -- TN3151 says: "Unless you have a specific reason to use URLSession, use Network framework for new WebSocket code", but at this point I feel that's bad advice.)

NWProtocolWebSocket: How to get the HTTP error?
 
 
Q