title: client program crashes when connect to server after disconnect Qt
body: <p>I am implementing client side of TCP-Ip server based on Qt but the program crashes when I closed connection and start again by hitting connect button.       </p>
  
  <p><em>Overview of project</em>. I have Qwidget ( main application) with 2 lineEdit for user to input port number  and server Ip address.
  It also has 2 button that connect to server and disconnect. On hitting connect button , it will call constructor of client socket and call connectToHost.</p>
  
  <p><em>Testing</em>: Tried testing on same computer  with server running on port 6000 and ip address 127.0.0.1.</p>
  
  <p><em>Problem</em>: When I launch Client application.And input port number and address and hit connect button it connects successfully. And I can write to server successfully. then I click disconnect button and it disconnects successfully but after that if I connect again by clicking connect button it crashes. I know problem is with tcpSocket but have no idea how to fix it.</p>
  
  <p>clientAgent( QWidget appilciation ).h</p>
  
  <pre><code>namespace Ui {
  class ClientAgent;
  }
  
  class ClientAgent : public QWidget
  {
      Q_OBJECT
  
  public:
      explicit ClientAgent(QWidget *parent = 0);
      ~ClientAgent();
  
  private:
      Ui::ClientAgent  *ui;
      ClientForTest    *tcpSockForTest;
  
  // Qwidget declartion
  below
  ....
  
  private slots:
      void startClient();
      void stopClient();
  
  public slots:
      void getCliReqTextChanged();
  
  };
  </code></pre>
  
  <p>ClientAgent.cpp</p>
  
  <pre><code>ClientAgent::ClientAgent( QWidget *parent ) :
            QWidget(parent),
            ui( new Ui::ClientAgent )
  {
  
  // Widget declartion and initilisation
  // Layout design etc
  
     //define signals and slots for Client
    // signals and slots
  
    connect( btnStartClient,  SIGNAL( clicked() ), this, SLOT( startClient() ) );
    connect( btnStopClient,   SIGNAL( clicked() ), this, SLOT( stopClient() ) );
  
  // for write
  connect( lEditCliReq,  SIGNAL( textChanged(const QString&amp; ) ), this, SLOT( getCliReqTextChanged() ) );
  
    ui-&gt;setupUi( this );
  
  }
  
  ClientAgent::~ClientAgent()
  {
      delete ui;
  }
  
  
  void ClientAgent::startClient()
  {
    qDebug() &lt;&lt; " we are in strt Clinet";
    qDebug() &lt;&lt; " connecting ";
  
    // get server address and port number from GUI
  
    QString testAddr = lEditAddr-&gt;text();
    QString testPort = lEditPrt-&gt;text();
  
    tcpSockForTest = new ClientForTest( testAddr, testPort.toInt(), this );
  
    if( tcpSockForTest-&gt;connectToServer() == true )
     {
        lblCliStatus-&gt;setText(" connected ....");
     }
    else
     {
        lblCliStatus-&gt;setText(" failed to connect....");
     }
  
  }
  
  void ClientAgent::stopClient()
  {
    qDebug() &lt;&lt; " disconnecting ";
    tcpSockForTest-&gt;disconnectToServer();
    delete tcpSockForTest;
  }
  </code></pre>
  
  <p>ClientForTest.h</p>
  
  <pre><code>class ClientForTest : public QObject
  {
    Q_OBJECT
  public:
    ClientForTest( QString hostAddr, quint16 portNum, QObject *parent );
  
    bool connectToServer();
    void disconnectToServer();
    QString getStoreMsgFrmCliReq( ) const;
  
    void writeToTest(const QString&amp; stripCmd );
    void executeSignals();
  
      ~ClientForTest();
  
  signals:
    void sigSendData();
  
  public slots:
    void connectedToTest();
    void connectionClosedByServer();
    void error();
  
  private:
     QTcpSocket   *sockFortest;
     QString       hostName;
     quint16       portNumber;
     quint16       nextBlockSize;
     QString       storeLineEditMsg;// store message from lineEditCliReq;
  
    };
  </code></pre>
  
  <p>ClientForTest.cpp</p>
  
  <pre><code>ClientForTest::ClientForTest( QString hostAddr, quint16 portNum, QObject *parent ) :
                              hostName( hostAddr ),
                              portNumber( portNum ),
                              QObject( parent )
  {
    connect( sockFortest, SIGNAL( connected() ), this, SLOT( connectedToTest() ) );
    connect( sockFortest, SIGNAL( disconnected() ), this, SLOT( connectionClosedByServer() ) );
    connect( sockFortest, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( error() ) );
  
    storeLineEditMsg = "";
  }
  void ClientForTest::executeSignals()
  {
  // actually I need toplace itin constructor will do later on
   connect( this,  SIGNAL( sigSendData() ), this, SLOT( connectedToTest() ) );
  }
  
  ClientForTest::~ClientForTest()
  {
  
    sockFortest-&gt;close();
    delete sockFortest;
  }
  
  bool ClientForTest::connectToServer()
  {
    sockFortest = new QTcpSocket( this-&gt;parent() ); // COULD be Probelm here but how to fix it?
    sockFortest-&gt;connectToHost( hostName, portNumber );
    nextBlockSize = 0;
    executeSignals();
    return sockFortest-&gt;waitForConnected(1000);
  }
  
  void ClientForTest::disconnectToServer()
  {
    sockFortest-&gt;close();
    //sockFortest.close();
    qDebug() &lt;&lt; "in disconnect for Test ";
    emit updateLabelinParent( updateStatusDis );
  }
  
  
  void ClientForTest::connectionClosedByServer()
  {
  
    qDebug() &lt;&lt; "connection closed by server ";
    if( nextBlockSize != 0xFFFF )
    {
      disconnectToServer();
    }
  }
  
  void ClientForTest::error()
  {
    qDebug() &lt;&lt; "in error ";
    disconnectToServer();
  }
  
  void ClientForTest::writeToTest( const QString&amp; stripCmd )
  {
    storeLineEditMsg = stripCmd;
    if( sockFortest-&gt;state() == QTcpSocket::UnconnectedState )
    {
        sockFortest-&gt;close();
        delete sockFortest; 
        sockFortest = new QTcpSocket( this-&gt;parent() );
        if( connectToServer() == true )
        {
           qDebug() &lt;&lt; " YEEEE CONNECTED AGAIN finally ";
        }
    }
    if( sockFortest-&gt;state() == QTcpSocket::ConnectedState )
     {
        qDebug() &lt;&lt; " YEEEE CONNECTED";
        sigSendData();
     }
  }
  
  
  void ClientForTest::connectedToTest( )
  {
    QByteArray block;
    QDataStream out( &amp;block, QIODevice::WriteOnly );
  
    out.setVersion( QDataStream::Qt_4_3 );
  
    QString stripCmd = getStoreMsgFrmCliReq(); // received info for some other function I havnt shown that func here
  
     out &lt;&lt; quint16( 0 ) &lt;&lt; stripCmd;
  
    out.device()-&gt;seek( 0 );
  
    out &lt;&lt; quint16( block.size() - sizeof( quint16 ) );
  
    qDebug()&lt;&lt;" yeeeee connected state...";
  
    sockFortest-&gt;write( block );
  
    //reset storeLineEditMessage
    storeLineEditMsg.clear();
  }
  </code></pre>