The meaning of connect-with-error changes depending on whether you are waiting for the database to shut down or start up.  Also, the code to check if the DB is up exists in connect().