                     m2config = msg
                     cb null, "OK"
 
+            debug "Starting loop for m2 OK"
+
+            tries = 0
+
             okF = ->
-                m2rpc.request "OK", (err,msg) ->
-                    return okF() if err
-                    done()
+                tries += 1
+                m2rpc.request "OK", null, null, timeout:500, (err,msg) ->
+                    if err
+                        debug "m2 OK error. Tries: #{tries}"
+                        okF() if tries < 20
+                    else
+                        debug "m2 OK success after #{tries} tries"
+                        done()
+
             okF()
 
         # this is getting masked by listening for our OK above

        m2config = null

        before (done) ->
            this.timeout 5000
            m2 = cp.fork "./index.js", ["--handoff",master_config...]

            m2rpc = new RPC m2, functions:

# Test handing off a populated rewind buffer and active source from one
# master to another, as we would during a graceful restart. Test that
# the new master is listening on the correct ports.

debug = require("debug")("sm:tests:master_handoffs")

MasterMode      = $src "modes/master"
IcecastSource   = $src "util/icecast_source"
