I don't think this is necessarily always correct.  For one, you're assuming that the connect callback immediately closes the connection on error.