Index: trunk/lib/FlairCore/src/ConnectedSocket.cpp
===================================================================
--- trunk/lib/FlairCore/src/ConnectedSocket.cpp	(revision 234)
+++ trunk/lib/FlairCore/src/ConnectedSocket.cpp	(revision 238)
@@ -32,5 +32,5 @@
   char buffer[sizeof(uint16_t)];
   size_t alreadyReceived = 0;
-  Time remainingTimeout = timeout; // ms
+  Time remainingTimeout = timeout; // ns
   do {
     Time beforeTime = GetTime(); // ns
@@ -38,7 +38,6 @@
         RecvMessage(buffer + alreadyReceived,
                     sizeof(uint16_t) - alreadyReceived, remainingTimeout);
-    remainingTimeout -= (GetTime() - beforeTime) / 100000;
-    if ((received < 0) || (remainingTimeout < 0))
-      throw std::runtime_error("Timeout");
+    remainingTimeout -= GetTime() - beforeTime;
+    if ((received < 0) || (remainingTimeout < 0)) throw std::runtime_error("Timeout");
     alreadyReceived += received;
   } while (alreadyReceived != sizeof(uint16_t));
@@ -52,10 +51,10 @@
   char *buffer = (char *)&dataInNetworkEndianness;
   size_t alreadySent = 0;
-  Time remainingTimeout = timeout; // ms
+  Time remainingTimeout = timeout; // ns
   do {
     Time beforeTime = GetTime(); // ns
     ssize_t sent = SendMessage(
         buffer + alreadySent, sizeof(uint16_t) - alreadySent, remainingTimeout);
-    remainingTimeout -= (GetTime() - beforeTime) / 100000;
+    remainingTimeout -= GetTime() - beforeTime;
     if ((sent < 0) || (remainingTimeout < 0))
       throw std::runtime_error("Timeout");
@@ -67,5 +66,5 @@
   char buffer[sizeof(uint32_t)];
   size_t alreadyReceived = 0;
-  Time remainingTimeout = timeout; // ms
+  Time remainingTimeout = timeout; // ns
   do {
     Time beforeTime = GetTime(); // ns
@@ -73,5 +72,5 @@
         RecvMessage(buffer + alreadyReceived,
                     sizeof(uint32_t) - alreadyReceived, remainingTimeout);
-    remainingTimeout -= (GetTime() - beforeTime) / 100000;
+    remainingTimeout -= GetTime() - beforeTime;
     if ((received < 0) || (remainingTimeout < 0))
       throw std::runtime_error("Timeout");
@@ -87,10 +86,10 @@
   char *buffer = (char *)&dataInNetworkEndianness;
   size_t alreadySent = 0;
-  Time remainingTimeout = timeout; // ms
+  Time remainingTimeout = timeout; // ns
   do {
     Time beforeTime = GetTime(); // ns
     ssize_t sent = SendMessage(
         buffer + alreadySent, sizeof(uint32_t) - alreadySent, remainingTimeout);
-    remainingTimeout -= (GetTime() - beforeTime) / 100000;
+    remainingTimeout -= GetTime() - beforeTime;
     if ((sent < 0) || (remainingTimeout < 0))
       throw std::runtime_error("Timeout");
@@ -108,5 +107,5 @@
         RecvMessage(buffer + alreadyReceived, stringSize - alreadyReceived,
                     remainingTimeout);
-    remainingTimeout -= (GetTime() - beforeTime) / 100000;
+    remainingTimeout -= GetTime() - beforeTime;
     if ((received < 0) || (remainingTimeout < 0))
       throw std::runtime_error("Timeout");
Index: trunk/lib/FlairCore/src/ConnectedSocket.h
===================================================================
--- trunk/lib/FlairCore/src/ConnectedSocket.h	(revision 234)
+++ trunk/lib/FlairCore/src/ConnectedSocket.h	(revision 238)
@@ -53,5 +53,5 @@
   * \brief Returns a socket on a new incoming connexion
   *
-  * \param ConnectedSocket &listeningSocket
+  * \param timeout timeout (in nanoseconds)
   */
   virtual ConnectedSocket *Accept(
@@ -65,5 +65,5 @@
   * \param unsigned int port
   * \param const distantAddress
-  * \param timeout timeout (in milliseconds)
+  * \param timeout timeout (in nanoseconds)
   */
   virtual bool Connect(const unsigned int port,
@@ -71,9 +71,9 @@
 
   /*!
-  * \brief Send a message
+  * \brief Send a message waiting up to timeout ns
   *
   * \param message message
   * \param message_len message length
-  * \param timeout timeout (in milliseconds)
+  * \param timeout timeout (in nanoseconds)
   */
   virtual ssize_t SendMessage(const char *message, size_t message_len,
@@ -83,9 +83,9 @@
   * \brief Receive a message
   *
-  * Receive a message and wait up to timeout. \n
+  * Receive a message and wait up to timeout ns. \n
   *
   * \param buf buffer to put the message
   * \param buf_len buffer length
-  * \param timeout timeout (in milliseconds)
+  * \param timeout timeout (in nanoseconds)
   *
   * \return size of the received message
Index: trunk/lib/FlairCore/src/IODevice.h
===================================================================
--- trunk/lib/FlairCore/src/IODevice.h	(revision 234)
+++ trunk/lib/FlairCore/src/IODevice.h	(revision 238)
@@ -151,5 +151,4 @@
     void SetIsReady(bool status);
 
-private:
   /*!
   * \brief Update using provided datas
@@ -164,4 +163,5 @@
   virtual void UpdateFrom(const io_data *data) = 0;
 
+  private:
   class IODevice_impl *pimpl_;
 };
Index: trunk/lib/FlairCore/src/Semaphore.cpp
===================================================================
--- trunk/lib/FlairCore/src/Semaphore.cpp	(revision 234)
+++ trunk/lib/FlairCore/src/Semaphore.cpp	(revision 238)
@@ -24,7 +24,7 @@
 namespace core {
 
-Semaphore::Semaphore(const Object *parent, uint32_t initialValue, string name)
-    : Object(parent, name, "semaphore") {
-  pimpl_ = new Semaphore_impl(this, initialValue);
+Semaphore::Semaphore(const Object *parent, uint32_t initialValue, string name, Type type)
+    : Object(parent, name, "semaphore"),type(type) {
+  pimpl_ = new Semaphore_impl(this, name, initialValue,type);
 }
 
Index: trunk/lib/FlairCore/src/Semaphore.h
===================================================================
--- trunk/lib/FlairCore/src/Semaphore.h	(revision 234)
+++ trunk/lib/FlairCore/src/Semaphore.h	(revision 238)
@@ -31,4 +31,5 @@
 
 public:
+  enum class Type { anonymous, named };
   /*!
   * \brief Constructor
@@ -39,5 +40,5 @@
   * \param name name
   */
-  Semaphore(const Object *parent, uint32_t initialValue, std::string name = "");
+  Semaphore(const Object *parent, uint32_t initialValue, std::string name = "", Type type=Type::anonymous);
 
   /*!
@@ -74,4 +75,5 @@
 private:
   class Semaphore_impl *pimpl_;
+  Type type;
 };
 
Index: trunk/lib/FlairCore/src/Semaphore_impl.cpp
===================================================================
--- trunk/lib/FlairCore/src/Semaphore_impl.cpp	(revision 234)
+++ trunk/lib/FlairCore/src/Semaphore_impl.cpp	(revision 238)
@@ -25,5 +25,5 @@
 using namespace flair::core;
 
-Semaphore_impl::Semaphore_impl(Semaphore *self, uint32_t initialValue) {
+Semaphore_impl::Semaphore_impl(Semaphore *self, string name, uint32_t initialValue, Semaphore::Type &type):type(type) {
   this->self = self;
   int status;
@@ -32,5 +32,12 @@
   status = rt_sem_create(&semaphore, NULL, initialValue, S_FIFO);
 #else
-  status = sem_init(&semaphore, 0, initialValue);
+  if (type==Semaphore::Type::named) {
+    sem_name = "/" + name;
+    semaphore=sem_open(sem_name.c_str(),O_CREAT, 0666, initialValue);
+    if (semaphore==SEM_FAILED) status=errno; else status=0;
+  } else { //anonymous
+    semaphore=new sem_t;
+    status = sem_init(semaphore, 0, initialValue);
+  }
 #endif
   if (status != 0) {
@@ -46,5 +53,10 @@
   status = rt_sem_delete(&semaphore);
 #else
-  status = sem_destroy(&semaphore);
+  if (type==Semaphore::Type::named) {
+    status=sem_close(semaphore);
+    sem_unlink(sem_name.c_str());
+  } else { //anonymous
+    status = sem_destroy(semaphore);
+  }
 #endif
   if (status != 0) {
@@ -58,5 +70,5 @@
   return !rt_sem_p(&semaphore, TM_NONBLOCK);
 #else
-  return !sem_trywait(&semaphore);
+  return !sem_trywait(semaphore);
 #endif
 }
@@ -77,7 +89,7 @@
     	semTimeout.tv_nsec -= 1000000000ULL;
     }
-    status = sem_timedwait(&semaphore, &semTimeout);
+    status = sem_timedwait(semaphore, &semTimeout);
   } else {
-    status = sem_wait(&semaphore);
+    status = sem_wait(semaphore);
   }
 #endif
@@ -102,5 +114,5 @@
   status = rt_sem_v(&semaphore);
 #else
-  status = sem_post(&semaphore);
+  status = sem_post(semaphore);
 #endif
   if (status != 0) {
Index: trunk/lib/FlairCore/src/SharedMem.h
===================================================================
--- trunk/lib/FlairCore/src/SharedMem.h	(revision 234)
+++ trunk/lib/FlairCore/src/SharedMem.h	(revision 238)
@@ -40,6 +40,7 @@
   * \param parent parent
   * \param name name
-  * \param size size of the shared memory
-  * \param blockOnRead if true reading will block if nothing written
+  * \param size size of the shared memory:w
+   *
+  * \param type mutex or producerConsumer. Mutex type is for symmetrical use (default)
   */
   SharedMem(const Object *parent, std::string name, size_t size, Type type=Type::mutex);
Index: trunk/lib/FlairCore/src/SharedMem_impl.cpp
===================================================================
--- trunk/lib/FlairCore/src/SharedMem_impl.cpp	(revision 234)
+++ trunk/lib/FlairCore/src/SharedMem_impl.cpp	(revision 238)
@@ -26,7 +26,5 @@
 using namespace flair::core;
 
-SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, size_t size, SharedMem::Type &type):self(self),type(type),size(size),
-                               sem(self,1,"/" + name + "_mutex"), sem_producer(self,0,"/" + name + "_producer"),sem_consumer(self,0,"/" + name + "_consumer") {
-
+SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, size_t size, SharedMem::Type &type):self(self),type(type),size(size) {
 #ifdef __XENO__
   heap_binded = false;
@@ -52,4 +50,10 @@
 
 #else
+  if (type==SharedMem::Type::mutex) {
+      sem = new Semaphore(self,1,"/" + name + "_mutex",Semaphore::Type::named);
+  } else { //producerConsumer
+      sem_producer=new Semaphore(self,0,"/" + name + "_producer", Semaphore::Type::named);
+      sem_consumer=new Semaphore(self,0,"/" + name + "_consumer", Semaphore::Type::named);
+  }
   shm_name = "/" + name;
   fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666);
@@ -100,22 +104,32 @@
    }
 */
+    if (type==SharedMem::Type::mutex) {
+      delete sem;
+    } else {
+      delete sem_producer;
+      delete sem_consumer;
+    }
 #endif
 }
 
 void SharedMem_impl::ReaderReady() {
-  sem_consumer.ReleaseSemaphore();
+  if (type==SharedMem::Type::producerConsumer) {
+    sem_consumer->ReleaseSemaphore();
+  } else {
+    self->Warn("Called on a non producerConsumer type of shared memory! (this is seriously wrong)");
+  }
 }
 
 void SharedMem_impl::Write(const char *buf, size_t size) {
   if (type==SharedMem::Type::mutex) {
-    sem.GetSemaphore();
+    sem->GetSemaphore();
     memcpy(mem_segment, buf, size);
-    sem.ReleaseSemaphore();
+    sem->ReleaseSemaphore();
   } else if (type==SharedMem::Type::producerConsumer) {
-    //wait until consumer took the data away before writting a new one
+    //wait until consumer took the data away before writing a new one
     //but should not block if nobody's there to read
-    if (sem_consumer.TryGetSemaphore()) {
+    if (sem_consumer->TryGetSemaphore()) {
       memcpy(mem_segment, buf, size);
-      sem_producer.ReleaseSemaphore();
+      sem_producer->ReleaseSemaphore();
     }
   }
@@ -124,15 +138,15 @@
 bool SharedMem_impl::Read(char *buf, size_t size, Time nsTimeout) {
   if (type==SharedMem::Type::mutex) {
-    if (sem.GetSemaphore()) {
+    if (sem->GetSemaphore()) { //may block for ever
       memcpy(buf, mem_segment, size);
-      sem.ReleaseSemaphore();
+      sem->ReleaseSemaphore();
       return true;
     }
   } else if (type==SharedMem::Type::producerConsumer) {
-    if (sem_producer.GetSemaphore(nsTimeout)) {
+    if (sem_producer->GetSemaphore(nsTimeout)) { //if nobody ever writes we need to exit this after a while
       memcpy(buf, mem_segment, size);
-      sem_consumer.ReleaseSemaphore();
+      sem_consumer->ReleaseSemaphore();
       return true;
-    }
+    };
   }
   return false;
Index: trunk/lib/FlairCore/src/TcpSocket.cpp
===================================================================
--- trunk/lib/FlairCore/src/TcpSocket.cpp	(revision 234)
+++ trunk/lib/FlairCore/src/TcpSocket.cpp	(revision 238)
@@ -22,4 +22,5 @@
 #include <fcntl.h>
 #include <string.h>
+#include <system_error>
 
 using std::string;
@@ -30,5 +31,5 @@
 TcpSocket::TcpSocket(const Object *parent, const std::string name,
                      bool _blockOnSend, bool _blockOnReceive)
-    : ConnectedSocket(parent, name), isConnected(false) {
+    : ConnectedSocket(parent, name), socket(0), isConnected(false), isListening(false), distantPort(0), distantAddress() {
   blockOnSend = _blockOnSend;
   blockOnReceive = _blockOnReceive;
@@ -37,5 +38,5 @@
 TcpSocket::~TcpSocket() {
   // Info("Debug: destroying TCP socket %s", ObjectName().c_str());
-  close(socket);
+  if (socket) close(socket);
 }
 
@@ -59,14 +60,22 @@
   }
 
-  listen(socket, 1);
+  int ret=listen(socket, 1);
+  if (ret < 0) {
+    char errorMsg[256];
+    Err("select: %s\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
+  } else {
+    isListening=true;
+  }
 }
 
 TcpSocket *TcpSocket::Accept(Time timeout) {
+  if (!isListening) throw std::logic_error("Can't call Accept on a non listening socket");
+
   TcpSocket *acceptedSocket = nullptr;
 
   struct timeval tv;
   if (timeout != 0) {
-    tv.tv_sec = timeout / 1000; // timeout is in ms
-    tv.tv_usec = (timeout % 1000) * 1000;
+    tv.tv_sec = timeout / 1000000000;
+    tv.tv_usec = (timeout % 1000000000) / 1000;
   }
   fd_set rset;
@@ -81,5 +90,5 @@
     if (ret == 0) {
       // timeout reached
-      // Err("timeout reached\n");
+      throw std::runtime_error("Timeout");
     } else {
       // our socket is readable, a new connection can be accepted
@@ -88,10 +97,10 @@
       sockaddr_in their_addr;
       socklen_t namelen = sizeof(their_addr);
-      if ((acceptedSocket->socket =
-               accept(socket, (sockaddr *)&their_addr, &namelen)) < 0) {
-        char errorMsg[256];
-        Err("error: %s\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
+      ret = accept(socket, (sockaddr *)&their_addr, &namelen);
+      if (ret < 0) {
         delete acceptedSocket;
-        acceptedSocket = nullptr;
+        throw std::system_error(std::error_code(ret, std::generic_category()));
+      } else {
+        acceptedSocket->socket = ret;
       }
     }
@@ -101,19 +110,27 @@
 }
 
-bool TcpSocket::Connect(const unsigned int distantPort,
-                        const std::string distantAddress, Time timeout) {
+bool TcpSocket::Connect(const unsigned int _distantPort,
+                        const std::string _distantAddress, Time timeout) {
   bool success = false;
-
+  bool criticalError=false; //meaning: if true then this socket can't be re-used
+
+  //should we open a new socket, close it, or re-use it?
   if (isConnected) {
-    if (this->distantPort == distantPort &&
-        this->distantAddress == distantAddress) {
+    // if already connected to the same host, just return true
+    if (distantPort == _distantPort && distantAddress == _distantAddress) {
       return true;
-    } else {
+    }
+    // if already connected, but to a different host, close the current socket
+    else {
+      isConnected=false;
       close(socket);
-    }
-  }
-  socket = ::socket(AF_INET, SOCK_STREAM, 0);
-  if (socket == -1)
-    return false;
+      socket=0;
+    }
+  }
+  if (!socket) {
+    socket = ::socket(AF_INET, SOCK_STREAM, 0);
+    if (socket == -1)
+      return false;
+  }
 
   sockaddr_in serv_addr;
@@ -123,4 +140,5 @@
     printf("incorrect network address.");
     close(socket);
+    socket=0;
     return false;
   }
@@ -135,5 +153,5 @@
       Err("socket connect: %s\n",
           strerror_r(errno, errorMsg, sizeof(errorMsg)));
-      success = false;
+      criticalError = true;
     } else {
       // now block with a timeout
@@ -152,12 +170,8 @@
         char errorMsg[256];
         Err("select: %s\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
-        success = false;
+        criticalError = true;
       } else {
-        if (ret == 0) {
-          // timeout reached
-          // Err("timeout reached\n");
-          success = false;
-        } else {
-          // something happened on our socket. Check if an error occured
+        if (ret != 0) { //if ret==0 we're on timeout (nothing to do since success is already set to false)
+          // something happened on our socket. Check if an error occurred
           int error;
           socklen_t len = sizeof(error);
@@ -165,16 +179,18 @@
             //char errorMsg[256];
             // Err("getsockopt: %s\n",strerror_r(errno,errorMsg,sizeof(errorMsg)));
-            success = false;
+            criticalError = true;
           } else if (error != 0) {
             //char errorMsg[256];
             // Err("socket error: %d(%s)\n",error,strerror_r(error,errorMsg,sizeof(errorMsg)));
-            success = false;
+            criticalError = true;
           } else {
-            if (connect(socket, (sockaddr *)&serv_addr, sizeof(serv_addr)) ==
-                -1) {
-              success = false;
-            } else {
+            if (connect(socket, (sockaddr *)&serv_addr, sizeof(serv_addr)) != -1) {
               // Info("connected indeed ^^\n");
               success = true;
+            } else {
+              //connect failed
+              if ((errno != EINPROGRESS) && (errno != EAGAIN)) {
+                criticalError = true;
+              }
             }
           }
@@ -183,5 +199,5 @@
     }
   } else {
-    success = true; // never reached suprisingly...
+    success = true; // should never happen since we go non blocking and connect can't be immediate
   }
   // switch back to blocking mode (default)
@@ -189,11 +205,15 @@
 
   if (!success) {
-    close(socket);
+    if (criticalError) {
+      close(socket);
+      socket=0;
+    }
+    isConnected=false;
      //Info("Debug: Connect to %s:%d failed\n", distantAddress.c_str(), distantPort);
     return false;
   } else {
     isConnected = true;
-    this->distantPort = distantPort;
-    this->distantAddress = distantAddress;
+    distantPort = _distantPort;
+    distantAddress = _distantAddress;
      //Info("Debug: Connect to %s:%d succeeded\n", distantAddress.c_str(), distantPort);
     return true;
@@ -208,6 +228,6 @@
   } else {
     struct timeval tv;
-    tv.tv_sec = timeout / 1000; // timeout is in ms
-    tv.tv_usec = (timeout % 1000) * 1000;
+    tv.tv_sec = timeout / 1000000000; // timeout is in ns
+    tv.tv_usec = (timeout % 1000000000) / 1000;
 
     setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
@@ -225,6 +245,6 @@
   } else {
     struct timeval tv;
-    tv.tv_sec = timeout / 1000; // timeout is in ms
-    tv.tv_usec = (timeout % 1000) * 1000;
+    tv.tv_sec = timeout / 1000000000; // timeout is in ns
+    tv.tv_usec = (timeout % 1000000000) / 1000;
 
     setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
Index: trunk/lib/FlairCore/src/TcpSocket.h
===================================================================
--- trunk/lib/FlairCore/src/TcpSocket.h	(revision 234)
+++ trunk/lib/FlairCore/src/TcpSocket.h	(revision 238)
@@ -30,11 +30,9 @@
   void Listen(const unsigned int port, const std::string localAddress = "ANY");
   TcpSocket *Accept(
-      Time timeout = 0); // should throw an exception if not a listening socket
+      Time timeout = TIME_INFINITE);
   bool Connect(const unsigned int distantPort, const std::string distantAddress,
-               Time timeout = 0); // timeout in milliseconds
-  ssize_t SendMessage(const char *message, size_t message_len,
-                      Time timeout = 0); // timeout in milliseconds
-  ssize_t RecvMessage(char *buf, size_t buf_len,
-                      Time timeout = 0); // timeout in milliseconds
+               Time timeout = TIME_INFINITE);
+  ssize_t SendMessage(const char *message, size_t message_len, Time timeout = TIME_INFINITE);
+  ssize_t RecvMessage(char *buf, size_t buf_len, Time timeout = TIME_INFINITE);
 
   uint16_t NetworkToHost16(uint16_t data);
@@ -48,6 +46,8 @@
   bool blockOnReceive;
   bool isConnected;
+  bool isListening;
   unsigned int distantPort;
   std::string distantAddress;
+  char *sendRingBuffer, *recvRingBuffer;
 };
 
Index: trunk/lib/FlairCore/src/unexported/Semaphore_impl.h
===================================================================
--- trunk/lib/FlairCore/src/unexported/Semaphore_impl.h	(revision 234)
+++ trunk/lib/FlairCore/src/unexported/Semaphore_impl.h	(revision 238)
@@ -17,4 +17,5 @@
 #include <native/sem.h>
 #else
+#include <fcntl.h>
 #include <semaphore.h>
 #endif
@@ -29,5 +30,5 @@
 class Semaphore_impl {
 public:
-  Semaphore_impl(flair::core::Semaphore *self, uint32_t initialValue);
+  Semaphore_impl(flair::core::Semaphore *self, std::string name, uint32_t initialValue, flair::core::Semaphore::Type &type);
   ~Semaphore_impl();
   bool TryGetSemaphore();
@@ -37,9 +38,11 @@
   RT_SEM semaphore;
 #else
-  sem_t semaphore;
+  std::string sem_name;
+  sem_t *semaphore;
 #endif
 
 private:
   flair::core::Semaphore *self;
+  flair::core::Semaphore::Type type;
 };
 
Index: trunk/lib/FlairCore/src/unexported/SharedMem_impl.h
===================================================================
--- trunk/lib/FlairCore/src/unexported/SharedMem_impl.h	(revision 234)
+++ trunk/lib/FlairCore/src/unexported/SharedMem_impl.h	(revision 238)
@@ -44,5 +44,6 @@
   size_t size;
   char *mem_segment;
-  flair::core::Semaphore sem,sem_producer,sem_consumer;
+  flair::core::Semaphore *sem; //for mutex type shared memory
+  flair::core::Semaphore *sem_producer,*sem_consumer; //sem_[produc|consum]er meaning = is [produc|consum]er ready?
 #ifdef __XENO__
   RT_HEAP heap;
