IOManager connect and accept failure handling. Fixes #3035

Cherry-picked from release-2.1.

I also made a couple of tiny improvements over the 2.1 fix, using a variable (`sock`) that is available, instead of accessing a property (`channel.socket`). Is it important to keep the changes consistent over versions?