Index: trunk/BNC/bncgetthread.cpp
===================================================================
--- trunk/BNC/bncgetthread.cpp	(revision 1347)
+++ trunk/BNC/bncgetthread.cpp	(revision 1348)
@@ -310,7 +310,10 @@
     // -----------------
     QString msg;
-    _socket = bncSocket::request(_mountPoint, _latitude, _longitude, 
-                                 _nmea, _timeOut, msg);
-    if (!_socket) {
+    delete _socket;
+    _socket = new bncSocket;
+    if (_socket->request(_mountPoint, _latitude, _longitude, 
+                         _nmea, _timeOut, msg) != success) {
+      delete _socket;
+      _socket = 0;
       return failure;
     }
@@ -900,5 +903,4 @@
   }
   while (1) {
-    delete _socket; _socket = 0;
     sleep(_nextSleep);
     if ( initRun() == success ) {
Index: trunk/BNC/bncrinex.cpp
===================================================================
--- trunk/BNC/bncrinex.cpp	(revision 1347)
+++ trunk/BNC/bncrinex.cpp	(revision 1348)
@@ -157,58 +157,60 @@
     QByteArray _longitude;
     QByteArray _nmea;
-    bncSocket* socket = bncSocket::request(url, _latitude, _longitude, _nmea, timeOut, msg);
-
-    if (socket) {
-      _headerLines.clear();
-      bool firstLineRead = false;
-      while (true) {
-        if (socket->canReadLine()) {
-          QString line = socket->readLine();
-          line.chop(1);
-          if (line.indexOf("MARKER NAME") != -1) {
-            irc = success;
-          }
-          if (line.indexOf("RINEX VERSION") != -1) {
-            if (_rinexVers == 3) {
-              _headerLines.append("     3.00           OBSERVATION DATA"
-                                  "    M (MIXED)"
-                                  "           RINEX VERSION / TYPE");
-            }
-            else {
-              _headerLines.append("     2.11           OBSERVATION DATA"
-                                  "    M (MIXED)"
-                                  "           RINEX VERSION / TYPE");
-            }
-            _headerLines.append("PGM / RUN BY / DATE");
-            firstLineRead = true;
-          }
-          else if (firstLineRead) {
-            if (line.indexOf("END OF HEADER") != -1) {
-              _headerLines.append("# / TYPES OF OBSERV");
-              if (_rinexVers == 2) {
-                _headerLines.append(
-                      QString("     1     1").leftJustified(60, ' ', true) +
-                      "WAVELENGTH FACT L1/2");
-              }
-              _headerLines.append("TIME OF FIRST OBS");
-              _headerLines.append( line );
-              break;
-            }
-            else {
-              _headerLines.append( line );
-            }
-          }
-        }
-        else {
-          socket->waitForReadyRead(timeOut);
-          if (socket->bytesAvailable() > 0) {
-            continue;
+    bncSocket* socket = new bncSocket();
+    if (socket->request(url, _latitude, _longitude, 
+                        _nmea, timeOut, msg) != success) {
+      delete socket;
+      return failure;
+    }
+
+    _headerLines.clear();
+    bool firstLineRead = false;
+    while (true) {
+      if (socket->canReadLine()) {
+        QString line = socket->readLine();
+        line.chop(1);
+        if (line.indexOf("MARKER NAME") != -1) {
+          irc = success;
+        }
+        if (line.indexOf("RINEX VERSION") != -1) {
+          if (_rinexVers == 3) {
+            _headerLines.append("     3.00           OBSERVATION DATA"
+                                "    M (MIXED)"
+                                "           RINEX VERSION / TYPE");
           }
           else {
+            _headerLines.append("     2.11           OBSERVATION DATA"
+                                "    M (MIXED)"
+                                "           RINEX VERSION / TYPE");
+          }
+          _headerLines.append("PGM / RUN BY / DATE");
+          firstLineRead = true;
+        }
+        else if (firstLineRead) {
+          if (line.indexOf("END OF HEADER") != -1) {
+            _headerLines.append("# / TYPES OF OBSERV");
+            if (_rinexVers == 2) {
+              _headerLines.append(
+                    QString("     1     1").leftJustified(60, ' ', true) +
+                    "WAVELENGTH FACT L1/2");
+            }
+            _headerLines.append("TIME OF FIRST OBS");
+            _headerLines.append( line );
             break;
           }
-        }
-      }
-      delete socket;
+          else {
+            _headerLines.append( line );
+          }
+        }
+      }
+      else {
+        socket->waitForReadyRead(timeOut);
+        if (socket->bytesAvailable() > 0) {
+          continue;
+        }
+        else {
+          break;
+        }
+      }
     }
   }
Index: trunk/BNC/bncsocket.cpp
===================================================================
--- trunk/BNC/bncsocket.cpp	(revision 1347)
+++ trunk/BNC/bncsocket.cpp	(revision 1348)
@@ -26,6 +26,6 @@
 // Constructor
 ////////////////////////////////////////////////////////////////////////////
-bncSocket::bncSocket(QTcpSocket* socket) {
-  _socket = socket;
+bncSocket::bncSocket() {
+  _socket = 0;
 }
 
@@ -33,4 +33,5 @@
 ////////////////////////////////////////////////////////////////////////////
 bncSocket::~bncSocket() {
+  delete _socket;
 }
 
@@ -39,5 +40,7 @@
 void bncSocket::connectToHost(const QString &hostName, quint16 port, 
                               QIODevice::OpenMode mode) {
-  _socket->connectToHost(hostName, port, mode);
+  if (_socket) {
+    _socket->connectToHost(hostName, port, mode);
+  }
 }
 
@@ -45,5 +48,10 @@
 ////////////////////////////////////////////////////////////////////////////
 bool bncSocket::waitForConnected(int msecs) {
-  return _socket->waitForConnected(msecs);
+  if (_socket) {
+    return _socket->waitForConnected(msecs);
+  }
+  else {
+    return false;
+  }
 }
 
@@ -51,5 +59,10 @@
 ////////////////////////////////////////////////////////////////////////////
 QAbstractSocket::SocketState bncSocket::state() const {
-  return _socket->state();
+  if (_socket) {
+    return _socket->state();
+  }
+  else {
+    return QAbstractSocket::UnconnectedState;
+  }
 }
 
@@ -57,5 +70,7 @@
 ////////////////////////////////////////////////////////////////////////////
 void bncSocket::close() {
-  _socket->close();
+  if (_socket) {
+    _socket->close();
+  }
 }
 
@@ -63,5 +78,10 @@
 ////////////////////////////////////////////////////////////////////////////
 qint64 bncSocket::bytesAvailable() const {
-  return _socket->bytesAvailable();
+  if (_socket) {
+    return _socket->bytesAvailable();
+  }
+  else {
+    return 0;
+  }
 }
 
@@ -69,5 +89,10 @@
 ////////////////////////////////////////////////////////////////////////////
 bool bncSocket::canReadLine() const {
-  return _socket->canReadLine();
+  if (_socket) {
+    return _socket->canReadLine();
+  }
+  else {
+    return false;
+  }
 }
 
@@ -75,5 +100,10 @@
 ////////////////////////////////////////////////////////////////////////////
 QByteArray bncSocket::readLine(qint64 maxlen) {
-  return _socket->readLine(maxlen);
+  if (_socket) {
+    return _socket->readLine(maxlen);
+  }
+  else {
+    return "";
+  }
 }
 
@@ -81,5 +111,10 @@
 ////////////////////////////////////////////////////////////////////////////
 bool bncSocket::waitForReadyRead(int msecs) {
-  return _socket->waitForReadyRead(msecs);
+  if (_socket) {
+    return _socket->waitForReadyRead(msecs);
+  }
+  else {
+    return false;
+  }
 }
 
@@ -87,5 +122,10 @@
 ////////////////////////////////////////////////////////////////////////////
 qint64 bncSocket::read(char* data, qint64 maxlen) {
-  return _socket->read(data, maxlen);
+  if (_socket) {
+    return _socket->read(data, maxlen);
+  }
+  else {
+    return -1;
+  }
 }
 
@@ -93,5 +133,10 @@
 ////////////////////////////////////////////////////////////////////////////
 qint64 bncSocket::write(const char* data, qint64 len) {
-  return _socket->write(data, len);
+  if (_socket) {
+    return _socket->write(data, len);
+  } 
+  else {
+    return -1;
+  }
 }
 
@@ -99,13 +144,20 @@
 ////////////////////////////////////////////////////////////////////////////
 bool bncSocket::waitForBytesWritten(int msecs) {
-  return _socket->waitForBytesWritten(msecs);
+  if (_socket) {
+    return _socket->waitForBytesWritten(msecs);
+  }
+  else {
+    return false;
+  }
 }
 
 // Connect to Caster, send the Request (static)
 ////////////////////////////////////////////////////////////////////////////
-bncSocket* bncSocket::request(const QUrl& mountPoint,
-                              QByteArray& latitude, QByteArray& longitude,
-                              QByteArray& nmea, int timeOut, 
-                              QString& msg) {
+t_irc bncSocket::request(const QUrl& mountPoint, const QByteArray& latitude, 
+                         const QByteArray& longitude, const QByteArray& nmea,
+                         int timeOut, QString& msg) {
+
+  delete _socket;
+  _socket = new QTcpSocket();
 
   // Connect the Socket
@@ -115,15 +167,15 @@
   int     proxyPort = settings.value("proxyPort").toInt();
  
-  bncSocket* socket = new bncSocket(new QTcpSocket());
   if ( proxyHost.isEmpty() ) {
-    socket->connectToHost(mountPoint.host(), mountPoint.port());
-  }
-  else {
-    socket->connectToHost(proxyHost, proxyPort);
-  }
-  if (!socket->waitForConnected(timeOut)) {
+    _socket->connectToHost(mountPoint.host(), mountPoint.port());
+  }
+  else {
+    _socket->connectToHost(proxyHost, proxyPort);
+  }
+  if (!_socket->waitForConnected(timeOut)) {
     msg += "Connect timeout\n";
-    delete socket;
-    return 0;
+    delete _socket; 
+    _socket = 0;
+    return failure;
   }
 
@@ -205,13 +257,14 @@
   msg += reqStr;
 
-  socket->write(reqStr, reqStr.length());
-
-  if (!socket->waitForBytesWritten(timeOut)) {
+  _socket->write(reqStr, reqStr.length());
+
+  if (!_socket->waitForBytesWritten(timeOut)) {
     msg += "Write timeout\n";
-    delete socket;
-    return 0;
-  }
-
-  return socket;
-}
-
+    delete _socket;
+    _socket = 0;
+    return failure;
+  }
+
+  return success;
+}
+
