Reimplemented wait() without using select().