Support wait_timeout

This is #151 rebased on top of the latest master.