     client.live = 'bar1\n' + client.live
     server.live = 'bar2\n'
     status()
-    client.push_edits()
-    server.push_edits()
+    while client.live != server.live
+        try
+            client.push_edits()
+            server.push_edits()
+            status()
+        catch e
+            status()
+            break
     status()
 
 exports.DSync = DSync

        if edits.edits.length > 0
            edits.shadow_version = @shadow_version
            console.log("#{@id} -- push_edits -- #{misc.to_json(edits)}")
            @edit_stack.push(edits)
            @shadow = @_copy(@live)
            @shadow_version += 1
        else
            console.log("#{@id} -- push_edits -- (nothing new)")

        # Push any remaining edits from the stack, *AND* report the last version received (also very important).
        @server.recv_edits(@edit_stack, @last_version_received, cb)

    # Receive and process the edits from the other end of the sync connection.
    recv_edits: (edit_stack, last_version_ack, cb) =>
        console.log("#{@id} -- recv_edits -- #{misc.to_json(edit_stack)}")
        # Keep only edits that we still need to send.
        @edit_stack = (edits for edits in @edit_stack when edits.shadow_version > last_version_ack)
        console.log("#{@id} -- remaining queued edits for next time: ", @edit_stack)
        # process the incoming edits
        for edits in edit_stack
            console.log("#{@id} -- must match: server shadow version = #{@shadow_version} and edit shadow version #{edits.shadow_version}")
            @last_version_received = edits.shadow_version
            @shadow = @_apply_edits(edits.edits, @shadow)
            @shadow_version += 1
            @live = @_apply_edits(edits.edits, @live)
            @backup_shadow = @_copy(@shadow)

        cb?()



exports.test1 = () ->
    client = new DSync(doc:"cat", id:"client")
    server = new DSync(doc:"cat", id:"server")
    client.connect(server)
    server.connect(client)

    client.live = "cats"
    server.live = "my\ncat"
    status = () ->
        console.log("------------------------")
        console.log(misc.to_json(client.status()))

        @last_version_received = 0
        @edit_stack            = []

    status: () =>
        return {'id':@id, 'live':@live, 'shadow':@shadow, 'shadow_version':@shadow_version, 'edit_stack':@edit_stack}

    restart: (reason) =>
        console.log("*********************************************************")
        console.log("* THINGS WENT TO HELL. -- #{reason} --  HAVE TO RESTART!!!!! *")
        console.log("*********************************************************")
        throw("dang")

    # Copy a document; strings are immutable and the default, so we
    # just return the object.
    _copy: (doc) =>
