diff -ru ../listentcp-ipv6-5084-3/twisted/internet/address.py ./twisted/internet/address.py
--- ../listentcp-ipv6-5084-3/twisted/internet/address.py	Thu Oct 13 17:35:12 2011
+++ ./twisted/internet/address.py	Thu Oct 13 18:16:40 2011
@@ -19,7 +19,7 @@
 
     @ivar type: A string describing the type of transport, either 'TCP' or
         'UDP'.
-    @ivar host: A string containing the dotted-quad IP address.
+    @ivar host: A string containing the native IP address.
     @ivar port: An integer representing the port number.
     """
 
@@ -47,6 +47,11 @@
 class IPv4Address(_IPAddress):
     """
     Object representing an IPv4 socket endpoint.
+    
+    @ivar type: A string describing the type of transport, either 'TCP' or
+        'UDP'.
+    @ivar host: A string containing the dotted-quad IP address.
+    @ivar port: An integer representing the port number.
     """
     def __init__(self, type, host, port, _bwHack=None):
         _IPAddress.__init__(self, type, host, port)
@@ -55,11 +60,44 @@
                     DeprecationWarning, stacklevel=2)
 
 
+    def __str__(self):
+        """
+        Returns human-readable IP address version.
+        
+        @return: A string containing the word "IPv6".
+        """
+        return "IPv4"
+
+
 
 class IPv6Address(_IPAddress):
     """
     Object representing an IPv6 socket endpoint.
+
+    @ivar type: A string describing the type of transport, either 'TCP' or
+        'UDP'.
+    @ivar host: A string containing the hexadecimal formatted IP address.
+    @ivar port: An integer representing the port number.
+    @ivar flow_info: An integer specifying IPv6 flow info flags.
+    @ivar scope_id: A string containing the address scope identifier.
     """
+    def __init__(self, type, host, port, flow_info=0, scope_id=0):
+        self.flow_info = flow_info
+        self.scope_id = scope_id
+        _IPAddress.__init__(self, type, host, port)
+
+
+    def __hash__(self):
+        return hash((self.type, self.host, self.port, self.flow_info, self.scope_id))
+
+
+    def __str__(self):
+        """
+        Returns human-readable IP address version.
+        
+        @return: A string containing the word "IPv6".
+        """
+        return "IPv6"
 
 

diff -ru ../listentcp-ipv6-5084-3/twisted/internet/endpoints.py ./twisted/internet/endpoints.py
--- ../listentcp-ipv6-5084-3/twisted/internet/endpoints.py	Thu Oct 13 17:35:12 2011
+++ ./twisted/internet/endpoints.py	Wed Oct 12 22:44:47 2011
@@ -25,6 +25,7 @@
 
 __all__ = ["clientFromString", "serverFromString",
            "TCP4ServerEndpoint", "TCP4ClientEndpoint",
+           "TCP6ServerEndpoint", "TCP6ClientEndpoint",
            "UNIXServerEndpoint", "UNIXClientEndpoint",
            "SSL4ServerEndpoint", "SSL4ClientEndpoint"]
 
@@ -145,9 +146,9 @@
 
 
 
-class TCP4ServerEndpoint(object):
+class _TCPServerEndpoint(object):
     """
-    TCP server endpoint with an IPv4 configuration
+    TCP server endpoint with an generic IP configuration
 
     @ivar _reactor: An L{IReactorTCP} provider.
 
@@ -188,9 +189,48 @@
 
 
 
-class TCP4ClientEndpoint(object):
+class TCP4ServerEndpoint(_TCPServerEndpoint):
     """
-    TCP client endpoint with an IPv4 configuration.
+    TCP server endpoint with an IPv4 configuration
+
+    @ivar _reactor: An L{IReactorTCP} provider.
+
+    @type _port: int
+    @ivar _port: The port number on which to listen for incoming connections.
+
+    @type _backlog: int
+    @ivar _backlog: size of the listen queue
+
+    @type _interface: str
+    @ivar _interface: the hostname to bind to, defaults to '' (all)
+    """
+    pass
+
+
+
+class TCP6ServerEndpoint(_TCPServerEndpoint):
+    """
+    TCP server endpoint with an IPv6 configuration
+
+    @ivar _reactor: An L{IReactorTCP} provider.
+
+    @type _port: int
+    @ivar _port: The port number on which to listen for incoming connections.
+
+    @type _backlog: int
+    @ivar _backlog: size of the listen queue
+
+    @type _interface: str
+    @ivar _interface: the hostname to bind to, defaults to '::' (all)
+    """
+    def __init__(self, reactor, port, backlog=50, interface='::'):
+        _TCPServerEndpoint.__init__(self, reactor, port, backlog, interface)
+
+
+
+class _TCPClientEndpoint(object):
+    """
+    TCP client endpoint with an generic IP configuration.
 
     @ivar _reactor: An L{IReactorTCP} provider.
 
@@ -247,9 +287,57 @@
 
 
 
-class SSL4ServerEndpoint(object):
+class TCP4ClientEndpoint(_TCPClientEndpoint):
     """
-    SSL secured TCP server endpoint with an IPv4 configuration.
+    TCP client endpoint with an IPv4 configuration.
+
+    @ivar _reactor: An L{IReactorTCP} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _timeout: int
+    @ivar _timeout: number of seconds to wait before assuming the
+        connection has failed.
+
+    @type _bindAddress: tuple
+    @type _bindAddress: a (host, port) tuple of local address to bind
+        to, or None.
+    """
+    pass
+
+
+
+class TCP6ClientEndpoint(_TCPClientEndpoint):
+    """
+    TCP client endpoint with an IPv6 configuration.
+
+    @ivar _reactor: An L{IReactorTCP} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _timeout: int
+    @ivar _timeout: number of seconds to wait before assuming the
+        connection has failed.
+
+    @type _bindAddress: tuple
+    @type _bindAddress: a (host, port) tuple of local address to bind
+        to, or None.
+    """
+    pass
+
+
+
+class _SSLServerEndpoint(object):
+    """
+    SSL secured TCP server endpoint with an generic IP configuration.
 
     @ivar _reactor: An L{IReactorSSL} provider.
 
@@ -303,9 +391,62 @@
 
 
 
-class SSL4ClientEndpoint(object):
+class SSL4ServerEndpoint(_SSLServerEndpoint):
     """
-    SSL secured TCP client endpoint with an IPv4 configuration
+    SSL secured TCP server endpoint with an IPv4 configuration.
+
+    @ivar _reactor: An L{IReactorSSL} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _sslContextFactory: L{OpenSSLCertificateOptions}
+    @var _sslContextFactory: SSL Configuration information as an
+        L{OpenSSLCertificateOptions}
+
+    @type _backlog: int
+    @ivar _backlog: size of the listen queue
+
+    @type _interface: str
+    @ivar _interface: the hostname to bind to, defaults to '' (all)
+    """
+    pass
+
+
+
+class SSL6ServerEndpoint(_SSLServerEndpoint):
+    """
+    SSL secured TCP server endpoint with an IPv6 configuration.
+
+    @ivar _reactor: An L{IReactorSSL} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _sslContextFactory: L{OpenSSLCertificateOptions}
+    @var _sslContextFactory: SSL Configuration information as an
+        L{OpenSSLCertificateOptions}
+
+    @type _backlog: int
+    @ivar _backlog: size of the listen queue
+
+    @type _interface: str
+    @ivar _interface: the hostname to bind to, defaults to '::' (all)
+    """
+    pass
+
+
+
+
+class _SSLClientEndpoint(object):
+    """
+    SSL secured TCP client endpoint with an generic IP configuration
 
     @ivar _reactor: An L{IReactorSSL} provider.
 
@@ -368,6 +509,62 @@
             return wf._onConnection
         except:
             return defer.fail()
+
+
+
+class SSL4ClientEndpoint(_SSLClientEndpoint):
+    """
+    SSL secured TCP client endpoint with an IPv4 configuration
+
+    @ivar _reactor: An L{IReactorSSL} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _sslContextFactory: L{OpenSSLCertificateOptions}
+    @var _sslContextFactory: SSL Configuration information as an
+        L{OpenSSLCertificateOptions}
+
+    @type _timeout: int
+    @ivar _timeout: number of seconds to wait before assuming the
+        connection has failed.
+
+    @type _bindAddress: tuple
+    @ivar _bindAddress: a (host, port) tuple of local address to bind
+        to, or None.
+    """
+    pass
+
+
+
+class SSL6ClientEndpoint(_SSLClientEndpoint):
+    """
+    SSL secured TCP client endpoint with an IPv6 configuration
+
+    @ivar _reactor: An L{IReactorSSL} provider.
+
+    @type _host: str
+    @ivar _host: The hostname to connect to as a C{str}
+
+    @type _port: int
+    @ivar _port: The port to connect to as C{int}
+
+    @type _sslContextFactory: L{OpenSSLCertificateOptions}
+    @var _sslContextFactory: SSL Configuration information as an
+        L{OpenSSLCertificateOptions}
+
+    @type _timeout: int
+    @ivar _timeout: number of seconds to wait before assuming the
+        connection has failed.
+
+    @type _bindAddress: tuple
+    @ivar _bindAddress: a (host, port) tuple of local address to bind
+        to, or None.
+    """
+    pass
 
 

diff -ru ../listentcp-ipv6-5084-3/twisted/internet/tcp.py ./twisted/internet/tcp.py
--- ../listentcp-ipv6-5084-3/twisted/internet/tcp.py	Thu Oct 13 17:35:12 2011
+++ ./twisted/internet/tcp.py	Thu Oct 13 17:49:17 2011
@@ -258,6 +258,7 @@
     A base class for client TCP (and similiar) sockets.
     """
     _base = Connection
+    _addressType = address.IPv4Address
 
     addressFamily = socket.AF_INET
     socketType = socket.SOCK_STREAM
@@ -270,7 +271,7 @@
             Connection.__init__(self, skt, None, reactor)
             self.doWrite = self.doConnect
             self.doRead = self.doConnect
-            reactor.callLater(0, whenDone)
+            self.doConnect()
         else:
             reactor.callLater(0, self.failIfNotConnected, error)
 
@@ -312,15 +313,46 @@
         return s
 
     def resolveAddress(self):
-        if abstract.isIPAddress(self.addr[0]):
+        if abstract.isIPAddress(self.addr[0]) or abstract.isIPv6Address(self.addr[0]):
             self._setRealAddress(self.addr[0])
         else:
             d = self.reactor.resolve(self.addr[0])
             d.addCallbacks(self._setRealAddress, self.failIfNotConnected)
 
-    def _setRealAddress(self, address):
-        self.realAddress = (address, self.addr[1])
-        self.doConnect()
+    def _setRealAddress(self, addr):
+        """
+        Set the real IP address for this client.
+        Once the IP address is set, the socket is created using the correct
+        address family.
+        """
+        if abstract.isIPv6Address(addr):
+            self.addressFamily = socket.AF_INET6
+            self._addressType = address.IPv6Address
+        self.realAddress = (addr, self.addr[1])
+
+        # create the socket and wait finish init after that
+        self.initConnection()
+
+    def initConnection(self):
+        """
+        Initialize connection by creating the appropriate socket.
+        """
+        err = None
+        skt = None
+        result = True
+
+        try:
+            skt = self.createInternetSocket()
+        except socket.error, se:
+            err = error.ConnectBindError(se[0], se[1])
+            result = None
+        if result and self.bindAddress is not None:
+            try:
+                skt.bind(self.bindAddress)
+            except socket.error, se:
+                err = error.ConnectBindError(se[0], se[1])
+                result = None
+        self._finishInit(result, skt, err, self.reactor)
 
     def doConnect(self):
         """I connect the socket.
@@ -394,37 +426,27 @@
         # BaseClient.__init__ is invoked later
         self.connector = connector
         self.addr = (host, port)
+        self.bindAddress = bindAddress
+        self.reactor = reactor
 
-        whenDone = self.resolveAddress
-        err = None
-        skt = None
-
-        try:
-            skt = self.createInternetSocket()
-        except socket.error, se:
-            err = error.ConnectBindError(se[0], se[1])
-            whenDone = None
-        if whenDone and bindAddress is not None:
-            try:
-                skt.bind(bindAddress)
-            except socket.error, se:
-                err = error.ConnectBindError(se[0], se[1])
-                whenDone = None
-        self._finishInit(whenDone, skt, err, reactor)
+        # Do outstanding initialization when real address is resolved
+        self.resolveAddress()
 
     def getHost(self):
-        """Returns an IPv4Address.
+        """
+        Returns an L{IPv4Address} or L{IPv6Address}.
 
         This indicates the address from which I am connecting.
         """
-        return address.IPv4Address('TCP', *self.socket.getsockname())
+        return self._addressType('TCP', *self.socket.getsockname())
 
     def getPeer(self):
-        """Returns an IPv4Address.
+        """
+        Returns an L{IPv4Address} or L{IPv6Address}.
 
         This indicates the address that I am connected to.
         """
-        return address.IPv4Address('TCP', *self.realAddress)
+        return self._addressType('TCP', *self.realAddress)
 
     def __repr__(self):
         s = '<%s to %s at %x>' % (self.__class__, self.addr, unsignedID(self))
@@ -556,11 +578,12 @@
 
     def __repr__(self):
         if self._realPortNumber is not None:
-            return "<%s of %s on %s>" % (self.__class__, self.factory.__class__,
-                                         self._realPortNumber)
+            return "<%s of %s on %s (%s)>" % (self.__class__, self.factory.__class__,
+                                         self._realPortNumber, self._addressType)
         else:
             return "<%s of %s (not listening)>" % (self.__class__, self.factory.__class__)
 
+
     def createInternetSocket(self):
         s = base.BasePort.createInternetSocket(self)
         if platformType == "posix" and sys.platform != "cygwin":
@@ -588,7 +611,8 @@
         # reflect what the OS actually assigned us.
         self._realPortNumber = skt.getsockname()[1]
 
-        log.msg("%s starting on %s" % (self.factory.__class__, self._realPortNumber))
+        log.msg("%s starting on %s (%s)" % (self.factory.__class__, self._realPortNumber,
+                                            self._addressType))
 
         # The order of the next 6 lines is kind of bizarre.  If no one
         # can explain it, perhaps we should re-arrange them.
@@ -739,13 +763,38 @@
 
 
 class Connector(base.BaseConnector):
+    _addressType = address.IPv4Address
+
     def __init__(self, host, port, factory, timeout, bindAddress, reactor=None):
-        self.host = host
         if isinstance(port, types.StringTypes):
             try:
                 port = socket.getservbyname(port, 'tcp')
             except socket.error, e:
                 raise error.ServiceNameUnknownError(string="%s (%r)" % (e, port))
+        
+        self.host, self.port = host, port
+        
+        if abstract.isIPv6Address(host):
+            self._addressType = address.IPv6Address
+        elif not abstract.isIPAddress(host):
+            # do a host lookup to make sure we have the correct address family
+            try:
+                addressInfo = socket.getaddrinfo(host, port)
+            except socket.gaierror:
+                raise error.DNSLookupError(host)
+            else:
+                assert len(addressInfo) > 0
+
+                # Sort addressInfo. IPv4 addresses should be preferred over 
+                # IPv6 addresses to keep legacy applications working.
+                addressInfo = sorted(addressInfo, key=lambda fields: fields[0])
+             
+                if addressInfo[0][0] == socket.AF_INET6:
+                    self._addressType = address.IPv6Address
+
+                host, port = addressInfo[0][4][:2]
+        
+        self.host = host
         self.port = port
         self.bindAddress = bindAddress
         base.BaseConnector.__init__(self, factory, timeout, reactor)
@@ -754,4 +803,4 @@
         return Client(self.host, self.port, self.bindAddress, self, self.reactor)
 
     def getDestination(self):
-        return address.IPv4Address('TCP', self.host, self.port)
+        return self._addressType('TCP', self.host, self.port)
diff -ru ../listentcp-ipv6-5084-3/twisted/internet/test/test_tcp.py ./twisted/internet/test/test_tcp.py
--- ../listentcp-ipv6-5084-3/twisted/internet/test/test_tcp.py	Thu Oct 13 17:35:12 2011
+++ ./twisted/internet/test/test_tcp.py	Thu Oct 13 00:16:22 2011
@@ -20,7 +20,7 @@
     IResolverSimple, IConnector, IReactorFDSet)
 from twisted.internet.address import IPv4Address, IPv6Address
 from twisted.internet.defer import Deferred, DeferredList, succeed, fail, maybeDeferred
-from twisted.internet.endpoints import TCP4ServerEndpoint, TCP4ClientEndpoint
+from twisted.internet.endpoints import TCP4ServerEndpoint, TCP4ClientEndpoint, TCP6ServerEndpoint, TCP6ClientEndpoint
 from twisted.internet.protocol import ServerFactory, ClientFactory, Protocol
 from twisted.python.failure import Failure
 from twisted.python import log
@@ -114,8 +114,8 @@
     """
     def __init__(self, reactor):
         self.reactor = reactor
-
-
+    
+    
     def clientConnectionFailed(self, connector, reason):
         self.reactor.stop()
 
@@ -605,6 +605,85 @@
 
 
 
+class TCP6ClientTestsBuilder(ReactorBuilder, ConnectionTestsMixin):
+    """
+    Builder defining tests relating to L{IReactorTCP.connectTCP}, IPv6 version.
+    """
+    def serverEndpoint(self, reactor):
+        """
+        Create a L{TCP6ServerEndpoint} listening on localhost on a
+        TCP/IP-selected port.
+        """
+        return TCP6ServerEndpoint(reactor, 0, interface='::1')
+
+
+    def clientEndpoint(self, reactor, serverAddress):
+        """
+        Create a L{TCP6ClientEndpoint} which will connect to localhost
+        on the port given by C{serverAddress}.
+
+        @type serverAddress: L{IPv6Address}
+        """
+        return TCP6ClientEndpoint(reactor, '::1', serverAddress.port)
+
+
+    def test_interface(self):
+        """
+        L{IReactorTCP.connectTCP} returns an object providing L{IConnector}.
+        """
+        reactor = self.buildReactor()
+        connector = reactor.connectTCP("::1", 1234, ClientFactory())
+        self.assertTrue(verifyObject(IConnector, connector))
+
+
+    def test_clientConnectionFailedStopsReactor(self):
+        """
+        The reactor can be stopped by a client factory's
+        C{clientConnectionFailed} method.
+        """
+        host, port = findFreePort("::1", socket.AF_INET6)[:2]
+        reactor = self.buildReactor()
+        reactor.connectTCP(host, port, Stop(reactor))
+        self.runReactor(reactor)
+
+
+    def test_addresses(self):
+        """
+        A client's transport's C{getHost} and C{getPeer} return L{IPv6Address}
+        instances which give the hexadecimal formatted string form of the local
+        and remote endpoints of the connection respectively.
+        """
+        host, port = findFreePort("::1", socket.AF_INET6)[:2]
+        reactor = self.buildReactor()
+
+        server = reactor.listenTCP(
+            0, serverFactoryFor(Protocol), interface=host)
+        serverAddress = server.getHost()
+
+        addresses = {'host': None, 'peer': None}
+        class CheckAddress(Protocol):
+            def makeConnection(self, transport):
+                addresses['host'] = transport.getHost()
+                addresses['peer'] = transport.getPeer()
+                reactor.stop()
+
+        clientFactory = Stop(reactor)
+        clientFactory.protocol = CheckAddress
+        reactor.connectTCP(
+            '::1', server.getHost().port, clientFactory,
+            bindAddress=('::1', port))
+
+        self.runReactor(reactor)
+
+        self.assertEqual(
+            addresses['host'],
+            IPv6Address('TCP', '::1', port))
+        self.assertEqual(
+            addresses['peer'],
+            IPv6Address('TCP', '::1', serverAddress.port))
+
+
+
 class StreamTransportTestsMixin:
     """
     Mixin defining tests which apply to any port/connection based transport.
@@ -1024,6 +1103,7 @@
 
 
 globals().update(TCPClientTestsBuilder.makeTestCaseClasses())
+globals().update(TCP6ClientTestsBuilder.makeTestCaseClasses())
 globals().update(TCPPortTestsBuilder.makeTestCaseClasses())
 globals().update(TCPConnectionTestsBuilder.makeTestCaseClasses())
