'Return a L{Deferred} firing with a L{MyClientFactory} and
L{MyServerFactory} connected pair, and the listening C{Port}. The
particularity is that the server protocol has been obtained after doing
a C{adoptStreamConnection} against the original server connection.'

def getConnectedClientAndServer(self, reactor, interface, addressFamily):

    firstServer = MyServerFactory()
    firstServer.protocolConnectionMade = Deferred()
    server = MyServerFactory()
    server.protocolConnectionMade = Deferred()
    server.protocolConnectionLost = Deferred()
    client = MyClientFactory()
    client.protocolConnectionMade = Deferred()
    client.protocolConnectionLost = Deferred()
    path = mktemp(suffix='.sock', dir='.')
    port = reactor.listenUNIX(path, firstServer)
    def firstServerConnected(proto):
        reactor.removeReader(proto.transport)
        reactor.removeWriter(proto.transport)
        reactor.adoptStreamConnection(proto.transport.fileno(), AF_UNIX, server)
    firstServer.protocolConnectionMade.addCallback(firstServerConnected)
    lostDeferred = gatherResults([client.protocolConnectionLost, server.protocolConnectionLost])
    def stop(result):
        if reactor.running:
            reactor.stop()
        return result
    lostDeferred.addBoth(stop)
    deferred = Deferred()
    deferred.addErrback(stop)
    startDeferred = gatherResults([client.protocolConnectionMade, server.protocolConnectionMade])
    def start(protocols):
        (client, server) = protocols
        deferred.callback((client, server, port))
    startDeferred.addCallback(start)
    reactor.connectUNIX(port.getHost().name, client)
    return deferred
