Fix connection event error handling.

Commit 86a99c6b999671ed444711139db1937617e802a0 changed the way we
handle connection events to protect against spurious event loop
callbacks. Unfortunately, it turns out that calling connect() twice on
the same socket results in different behaviors depending on the platform
(even though it seems well defined in POSIX). On Windows this resulted
in the connection handling code being unable to react to connection
errors (such as connection refused), always hitting the timeout; on
Linux this resulted in spurious error messages about connect() returning
success.

The truth table used in this commit is based on the POSIX standard
itself and on empirical observation for platform-specific behaviors.
The new code should be correct for Linux, Windows, and any
POSIX-compliant platform.