Index: /trunk/BNC/bncapp.cpp
===================================================================
--- /trunk/BNC/bncapp.cpp	(revision 940)
+++ /trunk/BNC/bncapp.cpp	(revision 941)
@@ -393,22 +393,22 @@
 void bncApp::printGPSEph(gpsephemeris* ep, bool printFile) {
 
-  QString line;
-  QByteArray allLines;
+  QString lineV2;
+  QString lineV3;
 
   struct converttimeinfo cti;
   converttime(&cti, ep->GPSweek, ep->TOC);
-  if      (_rinexVers == 3) {
-    line.sprintf("G%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
+
+  lineV3.sprintf("G%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
                  ep->satellite, cti.year, cti.month, cti.day, cti.hour,
                  cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
                  ep->clock_driftrate);
-  }
-  else if (_rinexVers == 2) {
-    line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
+
+  lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
                  ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
                  cti.minute, (double) cti.second, ep->clock_bias, 
                  ep->clock_drift, ep->clock_driftrate);
-  }
-  allLines += line;
+
+  QString    line;
+  QByteArray allLines;
 
   line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", (double)ep->IODE,
@@ -449,7 +449,69 @@
   allLines += line;
 
+  printOutput(printFile, lineV2, lineV3, allLines);
+}
+
+// Print One Glonass Ephemeris
+////////////////////////////////////////////////////////////////////////////
+void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
+
+  int ww  = ep->GPSWeek;
+  int tow = ep->GPSTOW; 
+  struct converttimeinfo cti;
+
+  updatetime(&ww, &tow, ep->tb*1000, 0);
+  converttime(&cti, ww, tow);
+
+  int tk = ep->tk-3*60*60; 
+  if (tk < 0) {
+    tk += 86400;
+  }
+
+  QString lineV2;
+  QString lineV3;
+
+  lineV3.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
+                 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour, 
+                 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
+
+  lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
+                 ep->almanac_number, cti.year%100, cti.month, cti.day, 
+                 cti.hour, cti.minute, (double) cti.second, -ep->tau, 
+                 ep->gamma, (double) tk);
+  
+  QString    line;
+  QByteArray allLines;
+
+  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
+               ep->x_velocity, ep->x_acceleration, 
+               (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
+  allLines += line;
+   
+  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
+               ep->y_velocity, ep->y_acceleration, 
+               (double) ep->frequency_number);
+  allLines += line;
+  
+  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
+               ep->z_velocity, ep->z_acceleration, (double) ep->E);
+  allLines += line;
+
+  printOutput(printFile, lineV2, lineV3, allLines);
+}
+
+// Output
+////////////////////////////////////////////////////////////////////////////
+void bncApp::printOutput(bool printFile, const QString& lineV2, 
+                         const QString& lineV3,
+                         const QByteArray& allLines) {
   // Output into file
   // ----------------
   if (printFile && _ephStreamGPS) {
+    if (_rinexVers == 2) {
+      *_ephStreamGPS << lineV2.toAscii();
+    }
+    else {
+      *_ephStreamGPS << lineV3.toAscii();
+    }
     *_ephStreamGPS << allLines;
     _ephStreamGPS->flush();
@@ -463,78 +525,6 @@
       QTcpSocket* sock = is.next();
       if (sock->state() == QAbstractSocket::ConnectedState) {
-        if (sock->write(allLines) == -1) {
-          delete sock;
-          is.remove();
-        }
-      }
-      else if (sock->state() != QAbstractSocket::ConnectingState) {
-        delete sock;
-        is.remove();
-      }
-    }
-  }
-}
-
-// Print One Glonass Ephemeris
-////////////////////////////////////////////////////////////////////////////
-void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
-
-  QString line;
-  QByteArray allLines;
-
-  int ww  = ep->GPSWeek;
-  int tow = ep->GPSTOW; 
-  struct converttimeinfo cti;
-
-  updatetime(&ww, &tow, ep->tb*1000, 0);
-  converttime(&cti, ww, tow);
-
-  int tk = ep->tk-3*60*60; 
-  if (tk < 0) {
-    tk += 86400;
-  }
-
-  if      (_rinexVers == 3) {
-    line.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
-                 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour, 
-                 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
-  }
-  else if (_rinexVers == 2) {
-    line.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
-                 ep->almanac_number, cti.year%100, cti.month, cti.day, 
-                 cti.hour, cti.minute, (double) cti.second, -ep->tau, 
-                 ep->gamma, (double) tk);
-  }
-  allLines += line;
-  
-  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
-               ep->x_velocity, ep->x_acceleration, 
-               (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
-  allLines += line;
-   
-  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
-               ep->y_velocity, ep->y_acceleration, 
-               (double) ep->frequency_number);
-  allLines += line;
-  
-  line.sprintf("    %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
-               ep->z_velocity, ep->z_acceleration, (double) ep->E);
-  allLines += line;
-
-  // Output into file
-  // ----------------
-  if (printFile && _ephStreamGlonass) {
-    *_ephStreamGlonass << allLines;
-    _ephStreamGlonass->flush();
-  }
-
-  // Output into the socket
-  // ----------------------
-  if (_sockets) {
-    QMutableListIterator<QTcpSocket*> is(*_sockets);
-    while (is.hasNext()) {
-      QTcpSocket* sock = is.next();
-      if (sock->state() == QAbstractSocket::ConnectedState) {
-        if (sock->write(allLines) == -1) {
+        if (sock->write(lineV3.toAscii())   == -1 ||
+            sock->write(allLines)           == -1) {
           delete sock;
           is.remove();
Index: /trunk/BNC/bncapp.h
===================================================================
--- /trunk/BNC/bncapp.h	(revision 940)
+++ /trunk/BNC/bncapp.h	(revision 941)
@@ -56,4 +56,7 @@
     void printGPSEph(gpsephemeris* ep, bool printFile);
     void printGlonassEph(glonassephemeris* ep, bool printFile);
+    void printOutput(bool printFile, const QString& lineV2, 
+                     const QString& lineV3,
+                     const QByteArray& allLines);
 
     QFile*            _logFile;
