Avoid leaking error handlers by tracking open sockets

Instead of adding an error handler for each socket, keep track of the
sockets when created, and propagate errors.