Waiting until TCP socket is available in bash

Why don't you simply call ssh? It will wait until the remote host is available and time out after a while. If an error occurs, it will return 255, which means that you can retry it.