From ed0cf87e280e6e8d7f71200a97247184b7495eb3 Mon Sep 17 00:00:00 2001
From: laurentgom <laurentgom@4e206d99-4929-0410-ac5d-dfc041789085>
Date: Sun, 12 Jul 2009 12:12:48 +0000
Subject: [PATCH] Fixed TCP connect with timeout returning success when the
 connection failed

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1173 4e206d99-4929-0410-ac5d-dfc041789085
---
 src/SFML/Network/SocketTCP.cpp | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/SFML/Network/SocketTCP.cpp b/src/SFML/Network/SocketTCP.cpp
index 03d96d3b..6de4b081 100644
--- a/src/SFML/Network/SocketTCP.cpp
+++ b/src/SFML/Network/SocketTCP.cpp
@@ -131,11 +131,22 @@ Socket::Status SocketTCP::Connect(unsigned short Port, const IPAddress& HostAddr
             Time.tv_sec  = static_cast<long>(Timeout);
             Time.tv_usec = (static_cast<long>(Timeout * 1000) % 1000) * 1000;
 
-            // Wait for something to write on our socket (would mean the connection has been accepted)
+            // Wait for something to write on our socket (which means that the connection request has returned)
             if (select(static_cast<int>(mySocket + 1), NULL, &Selector, NULL, &Time) > 0)
-            {
-                // Connection succeeded
-                Status = Socket::Done;
+            {
+                // At this point the connection may have been either accepted or refused.
+                // To know whether it's a success or a failure, we try to retrieve the name of the connected peer
+                SocketHelper::LengthType Size = sizeof(SockAddr);
+                if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
+                {
+                    // Connection accepted
+                    Status = Socket::Done;
+                }
+                else
+                {
+                    // Connection failed
+                    Status = SocketHelper::GetErrorStatus();
+                }
             }
             else
             {