Use type checking to detect an errant closure

Inspired by http://twitter.com/jessemcnelis/status/476704848486731776 I decided to figure out a way to check for the socket closure without comparing strings.

It's a bit longer, but it's more correct.

Side note: I'm thinking maybe about moving this function to a separate package because this is such a common issue.