title: QTcpSocket client auto reconnect
body: <p>I'm trying to write a piece of code that periodically tries to connect to the server using QTcpSocket until the server is up and ready. The client should also automatically and periodically try reconnecting when the server is down until it is up again or user manually closes the program.</p>
  
  <p>What I have done is subscribing to the connected and error signals of QTcpSocket. When I catch the error signal, I basically call connectToHost method again. </p>
  
  <p>My code periodically tries to connect to the host until the server is ready (this part works fine). However, the problem is when the server is down it can never reconnect. When the connection is down I get RemoteHostClosedError as expected. But, after calling the connectToHost method again inside the same method (where I catch RemoteHostClosedError) I got nothing. Even the error signal is not emitted by the QTcpSocket object. </p>
  
  <p>I gave my code below.</p>
  
  <pre><code>TcpServerConnector::TcpServerConnector( SocketSettings socketSettings, QObject* parent)
  : QObject(parent), socket(new QTcpSocket())
  {
      this-&gt;connect(this-&gt;socket, SIGNAL(connected()), this, SLOT(connectionSuccess_Handler()), Qt::DirectConnection);
      this-&gt;connect(this-&gt;socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionError_Handler(QAbstractSocket::SocketError)), Qt::DirectConnection);
  }
  
  
  void TcpServerConnector::connectionError_Handler( QAbstractSocket::SocketError error )
  {
      switch (error)
      {
      case QAbstractSocket::AddressInUseError:
          this-&gt;logger.log(LogLevel::ERR, "SOCKET ERROR: Address is already in use");
          break;
      case QAbstractSocket::ConnectionRefusedError:
          this-&gt;logger.log(LogLevel::ERR, "SOCKET ERROR: Connection refused");
          break;
      case QAbstractSocket::HostNotFoundError:
          this-&gt;logger.log(LogLevel::ERR, "SOCKET ERROR: Host not found");
          break;
      case QAbstractSocket::RemoteHostClosedError:
          this-&gt;logger.log(LogLevel::ERR, "SOCKET ERROR: Remote host closed");            
          break;  
      }
  
  
      this-&gt;socket-&gt;abort();
      this-&gt;socket-&gt;close();
      this-&gt;logger.log(LogLevel::DEBUG, "Reconnecting...");
      SystemUtil::sleepCurrentThread(1000);
      this-&gt;socket-&gt;connectToHost(ip_address, port);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I check the state of the QTcpSocket before and after calling the connectToHost method (the last line I gave here). Before calling connectToHost the state is UnconnectedState and after calling connectToHost its state becomes Connecting. Nothing unexpected. However, neither it can connect to the server, nor emit an error signal.</p>
  
  <p>Any idea?</p>
  
  <p>Note: The connectToHost metod of QTcpSocket is called internally for the first time. </p>