Index: fact/FADctrl/FAD.cc
===================================================================
--- fact/FADctrl/FAD.cc	(revision 10117)
+++ fact/FADctrl/FAD.cc	(revision 10120)
@@ -68,6 +68,5 @@
 
   // Construct boards
-  BoardList = Tokenize(GetConfig("BoardList","129.217.160.119"));
-  BoardList = Tokenize("192.33.99.225");
+  BoardList = Tokenize(GetConfig("BoardList"));
 
   for (unsigned int i=0; i<BoardList.size(); i++) {
@@ -85,6 +84,5 @@
   int Ret;
   if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchEventThread,(void *) this)) != 0) {
-    Message(ERROR, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret));
-	Thread = pthread_self();
+    Message(FATAL, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret));
   }
 
@@ -105,7 +103,5 @@
 
   // Wait for DIM service thread to quit
-  if (pthread_equal(Thread, pthread_self()) == 0) {
-	if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret));
-  }
+  if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret));
 
   // Delete all boards (cancels threads automatically)
@@ -114,5 +110,5 @@
   delete Command;
   delete ConsoleOut;
-  free(ConsoleText);  
+  free(ConsoleText);
 }
 
@@ -208,6 +204,6 @@
   }
 
-  if (Match(Parameter[1],"com")) PrintMessage("all active boards switched to command mode - socket 0\n");
-  else if (Match(Parameter[1],"daq")) PrintMessage("all active boards switched to DAQ mode - socket 1..7\n");
+  if (Match(Parameter[1],"com")) PrintMessage("All active boards switched to command mode - socket 0\n");
+  else if (Match(Parameter[1],"daq")) PrintMessage("All active boards switched to DAQ mode - socket 1..7\n");
   else PrintUsage();
 } 
@@ -283,6 +279,6 @@
   }
 
-  if (Match(Parameter[1],"enable")) PrintMessage("all active boards accept now incoming triggers\n");
-  else if (Match(Parameter[1],"disable")) PrintMessage("no active board accepts any incoming trigger anymore.\n");
+  if (Match(Parameter[1],"enable")) PrintMessage("All active boards accept incoming triggers\n");
+  else if (Match(Parameter[1],"disable")) PrintMessage("No active board accepts incoming triggers\n");
 } 
 
Index: fact/FADctrl/FAD.h
===================================================================
--- fact/FADctrl/FAD.h	(revision 10117)
+++ fact/FADctrl/FAD.h	(revision 10120)
@@ -48,5 +48,4 @@
     int NumEvents;			// Number of event taken            
 	
-	std::vector<std::string> BoardList;
 	std::vector<class FADBoard *> Boards;
 
@@ -88,4 +87,5 @@
 	int Pipe[2];
     int NumEventsRequested;	// Number of events requested
+	std::vector<std::string> BoardList;
 };
 
Index: fact/FADctrl/FADBoard.cc
===================================================================
--- fact/FADctrl/FADBoard.cc	(revision 10117)
+++ fact/FADctrl/FADBoard.cc	(revision 10120)
@@ -23,8 +23,22 @@
   ACalibTime = -1;
   Status.Update.tv_sec = -1;
-  Thread = pthread_self(); // For checking in destructor
 
   Name = new char [Server.size()+1]; // Name in permanent memory for DIM service
   strcpy(Name, Server.c_str());
+
+  // Initialise mutex for synchronization
+  pthread_mutexattr_t Attr;
+
+  if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
+    m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
+  }
+  if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
+    m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret));
+  }
+
+  // Initialise condition variable for synchronization
+  if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
+    m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret));
+  }
 
   // Resolve hostname
@@ -37,5 +51,5 @@
   // Open socket descriptor
   if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
-    m->PrintMessage("Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno));
+    m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno));
     return;
   }
@@ -62,26 +76,12 @@
   DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);
 
-  // Initialise mutex for synchronization
-  pthread_mutexattr_t Attr;
-
-  if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
-    m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
-  }
-  if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
-    m->Message(m->ERROR, "pthread_mutex_init() failed (%s)", strerror(Ret));
-	return;
-  }
-
-  // Initialise condition variable for synchronization
-  if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
-    m->Message(m->ERROR, "pthread_cond_init() failed (%s)", strerror(Ret));
-	return;
-  }
-
   // Create thread that receives data
   if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) {
-    m->Message(m->ERROR, "pthread_create() failed in FADBoard() (%s)\n", strerror(Ret));
-	Thread = pthread_self();
-	return;
+    m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret));
+  }
+
+  // Create thread to connect to other sockets
+  if ((Ret = pthread_create(&OtherThread, NULL, (void * (*)(void *)) OpenOtherSockets, (void *) Name)) != 0) {
+    m->Message(m->FATAL, "pthread_create() failed for OpenOtherSockets (%s)", strerror(Ret));
   }
 
@@ -96,8 +96,34 @@
   int Ret;
 
-  // Cancel thread (if it did not quit already) and wait for it to quit
-  if (pthread_equal(Thread, pthread_self()) == 0) {
-	if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
-	if ((Ret = pthread_join(Thread, NULL)) != 0) m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
+  // Avoid segmentation faults by chekcing for InitOK
+  if (InitOK) {
+	// Terminate thread for other sockets  
+	if ((Ret = pthread_cancel(OtherThread)) != 0) {
+	  m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() for OtherThread (%s)", strerror(Ret));
+	}
+	if ((Ret = pthread_join(OtherThread, NULL)) != 0) {
+	  m->Message(m->ERROR, "pthread_join() failed in ~FADBoard for OtherThread (%s)", strerror(Ret));
+	}
+
+	// Cancel thread (if it did not quit already) and wait for it to quit
+	if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) {
+	  m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
+	}
+	if ((Ret = pthread_join(Thread, NULL)) != 0) {
+	  m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
+	}
+
+	delete DIM_Name;
+	delete DIM_ID;
+	delete DIM_Temp;
+	delete DIM_DAC;
+	delete DIM_ROI;
+  }
+  
+  delete[] Name;
+
+  // Close socket descriptor
+  if ((Socket != -1) && (close(Socket) == -1)) {
+	m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno));  
   }
 
@@ -108,18 +134,6 @@
 
   // Delete mutex  
-  if (InitOK && ((Ret = pthread_mutex_destroy(&Mutex)) != 0)) {
+  if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) {
 	m->Message(m->ERROR, "pthread_mutex_destroy() failed in ~FADBoard (%s)", strerror(Ret));
-  }
-
-  delete DIM_Name;
-  delete DIM_ID;
-  delete DIM_Temp;
-  delete DIM_DAC;
-  delete DIM_ROI;
-  delete[] Name;
-
-  // Close socket descriptor
-  if ((Socket != -1) && (close(Socket) == -1)) {
-	m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno));  
   }
 }
@@ -524,2 +538,69 @@
   }
 }
+
+
+//
+// OpenOtherSockets()
+//
+void FADBoard::OpenOtherSockets(char *Hostname) {
+
+  int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
+  int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
+  fd_set DescriptorList;
+  char Buffer[1000000];
+
+  // Resolve hostname
+  struct hostent *Host = gethostbyname(Hostname);
+  if (Host == 0) {
+    printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
+    return;
+  }
+
+  // Connect to server
+  struct sockaddr_in SocketAddress;
+  SocketAddress.sin_family = PF_INET;
+  SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
+
+  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
+	// Open socket descriptor
+	if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+      printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
+      return;
+	}
+
+	// Determine highest socket number for select()
+	if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
+	 
+	// Connect to server
+    SocketAddress.sin_port = htons((unsigned short) List[i]);
+	if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
+      printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
+      return;
+	}
+  }
+  
+  while(true) {
+    // Wait for data from terminal (stdin) or from sockets
+    FD_ZERO(&DescriptorList);   
+    for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
+    if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
+      perror("OtherSockets: Error with select()");
+      break;
+    }
+	
+	// Data from socket
+	for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
+	  Ret = read(Socket[i], Buffer, sizeof(Buffer));
+      if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
+      else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
+      else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
+    }
+  }
+
+  // Close all sockets
+  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
+	if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
+	  printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));  
+	}
+  }
+}
Index: fact/FADctrl/FADBoard.h
===================================================================
--- fact/FADctrl/FADBoard.h	(revision 10117)
+++ fact/FADctrl/FADBoard.h	(revision 10120)
@@ -25,14 +25,11 @@
 	class FAD *m;
 	int Socket;
-	pthread_t Thread;
+	pthread_t Thread, OtherThread;
 	pthread_mutex_t Mutex;
-	DimService *DIM_Name;
-	DimService *DIM_ID;
-	DimService *DIM_Temp;
-	DimService *DIM_ROI;
-	DimService *DIM_DAC;
+	DimService *DIM_Name, *DIM_ID, *DIM_Temp, *DIM_ROI, *DIM_DAC;
 
 	void ReadLoop();
 	static void LaunchThread(class FADBoard *);
+	static void OpenOtherSockets(char *);
 
   public: 
Index: fact/FADctrl/FADctrl.cc
===================================================================
--- fact/FADctrl/FADctrl.cc	(revision 10117)
+++ fact/FADctrl/FADctrl.cc	(revision 10120)
@@ -12,5 +12,4 @@
 
 const string READLINE_HIST_FILE = string(getenv("HOME"))+"/.history_FADctrl";
-void OpenOtherSockets();
 
 // ================
@@ -20,8 +19,6 @@
 int main() {
 
-  // Uses getc() for readline library (allows interruption by signal)
+  // Uses getc() for readline library (allows interruption by signal) and load history buffer
   rl_getc_function = getc;
-  
-  // Load history buffer
   read_history(READLINE_HIST_FILE.c_str());
 
@@ -35,47 +32,14 @@
   signal(SIGPIPE,SIG_IGN);
 
-  // Create thread to connect to other sockets
-  int RetVal;
-  pthread_t Thread;
-  if ((RetVal = pthread_create(&Thread, NULL, (void * (*)(void *)) OpenOtherSockets, NULL)) != 0) {
-    printf("pthread_create() failed for OpenOtherSockets(%s)\n", strerror(RetVal));
-	exit(EXIT_FAILURE);
-  }
-
   // Initialise all boards
-  M.PrintMessage("Initalizing all boards...\n");
-
   DimClient::sendCommand(SERVER_NAME"/Command", "dwrite off");
   DimClient::sendCommand(SERVER_NAME"/Command", "domino off");
-  sleep(1);
-
   DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 25000");
   DimClient::sendCommand(SERVER_NAME"/Command", "dac 1-3 0");
   DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800");
-  sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "roi all 10");
-  //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 29");
-  //sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
-  //sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 30");
-  //sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
-  //sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 0");
-  //sleep (1);
-
-  //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
   DimClient::sendCommand(SERVER_NAME"/Command", "domino on");
   DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on");
   DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024");
-
   DimClient::sendCommand(SERVER_NAME"/Command", "trigger enable");
-
   M.PrintMessage("Finished initalizing all boards\n");
   
@@ -104,79 +68,3 @@
   int Ret = write_history(READLINE_HIST_FILE.c_str());
   if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE.c_str(), strerror(Ret));
-
-  // Terminate thread for other sockets  
-  if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation in main() (%s)\n", strerror(Ret));
-  if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed in main () (%s)\n", strerror(Ret));
 }
-
-
-// ====================
-//   OpenOtherSockets()
-// ====================
-
-void OpenOtherSockets() {
-
-	//ETHZ
-  static char Hostname[] = "192.33.99.225";
-	//TUDO
-	//static char Hostname[] = "129.217.160.119";
-  int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
-  int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
-  fd_set DescriptorList;
-  char Buffer[1000000];
-
-  // Resolve hostname
-  struct hostent *Host = gethostbyname(Hostname);
-  if (Host == 0) {
-    printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
-    return;
-  }
-
-  // Connect to server
-  struct sockaddr_in SocketAddress;
-  SocketAddress.sin_family = PF_INET;
-  SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
-
-  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
-	// Open socket descriptor
-	if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
-      printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
-      return;
-	}
-
-	// Determine highest socket number for select()
-	if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
-	 
-	// Connect to server
-    SocketAddress.sin_port = htons((unsigned short) List[i]);
-	if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
-      printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
-      return;
-	}
-  }
-  
-  while(true) {
-    // Wait for data from terminal (stdin) or from sockets
-    FD_ZERO(&DescriptorList);   
-    for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
-    if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
-      perror("OtherSockets: Error with select()");
-      break;
-    }
-	
-	// Data from socket
-	for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
-	  Ret = read(Socket[i], Buffer, sizeof(Buffer));
-      if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
-      else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
-      else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
-    }
-  }
-
-  // Close all sockets
-  for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
-	if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
-	  printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));  
-	}
-  }
-}
Index: fact/FADctrl/History.txt
===================================================================
--- fact/FADctrl/History.txt	(revision 10117)
+++ fact/FADctrl/History.txt	(revision 10120)
@@ -8,2 +8,3 @@
 23/1/2011	Data can be written to disk in M0 format
 24/1/2011	Integrated amplitude calibration into FADBoard class
+27/1/2011	Moved OpenOtherSockets() to FADBoard class
