Add wait timeout

Added wait timeout. Sometimes for unknown reason, this function does not return at all. Timeout throws error instead of running forever.