Don't assume the existence of res.socket

We were getting this error `TypeError: Cannot read property 'encrypted' of null` and tracked it down to this code. 

Added a check to ensure that res.socket exists. Also made the source of these errors a bit clearer to save the next person a couple of hours...