Index: /XIOS/trunk/src/client.cpp
===================================================================
--- /XIOS/trunk/src/client.cpp	(revision 596)
+++ /XIOS/trunk/src/client.cpp	(revision 597)
@@ -201,7 +201,6 @@
         context->initClient(contextComm,contextInterComm, contextServer);
 
-
         // Secondly, initialize context on server side
-        contextServer->initServer(contextComm,contextInterComm);
+        contextServer->initServer(contextComm,contextInterComm, context);
 
         // Finally, we should return current context to context client
Index: /XIOS/trunk/src/context_client.cpp
===================================================================
--- /XIOS/trunk/src/context_client.cpp	(revision 596)
+++ /XIOS/trunk/src/context_client.cpp	(revision 597)
@@ -206,4 +206,6 @@
          free &= (*itBuffer)->isBufferFree(*itSize);
         }
+        if (!free)
+          context->server->listen();
       }
       CTimer::get("Blocking time").suspend();
Index: /XIOS/trunk/src/context_server.cpp
===================================================================
--- /XIOS/trunk/src/context_server.cpp	(revision 596)
+++ /XIOS/trunk/src/context_server.cpp	(revision 597)
@@ -52,4 +52,9 @@
   }
 
+  bool CContextServer::hasFinished(void)
+  {
+    return finished;
+  }
+
   bool CContextServer::eventLoop(void)
   {
@@ -79,5 +84,5 @@
         {
           it=buffers.find(rank);
-          if (it==buffers.end())
+          if (it==buffers.end()) // Receive the buffer size and allocate the buffer
           {
             StdSize buffSize = 0;
@@ -169,10 +174,10 @@
       if (event->isFull())
       {
-        if (!scheduled && !CXios::isServer)
+        if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side
         {
           CServer::eventScheduler->registerEvent(currentTimeLine,hashId);
           scheduled=true;
         }
-        else if (CXios::isServer || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) )
+        else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) )
         {
          CTimer::get("Process events").resume();
@@ -207,4 +212,5 @@
     if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
     {
+      finished=true;
       info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl;
       std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
@@ -218,5 +224,4 @@
       }
       context->finalize();
-      finished=true;
       report(0)<< " Memory report : Context <"<<context->getId()<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
     }
Index: /XIOS/trunk/src/context_server.hpp
===================================================================
--- /XIOS/trunk/src/context_server.hpp	(revision 596)
+++ /XIOS/trunk/src/context_server.hpp	(revision 597)
@@ -23,4 +23,5 @@
     void setPendingEvent(void) ;
     bool hasPendingEvent(void) ;
+    bool hasFinished(void);
 
     MPI_Comm intraComm ;
Index: /XIOS/trunk/src/interface/c/iccalendar.cpp
===================================================================
--- /XIOS/trunk/src/interface/c/iccalendar.cpp	(revision 596)
+++ /XIOS/trunk/src/interface/c/iccalendar.cpp	(revision 597)
@@ -13,5 +13,5 @@
     CTimer::get("XIOS").resume();
     xios::CContext* context = CContext::getCurrent();
-    if (!context->hasServer) context->client->checkBuffers();
+    if (!context->hasServer) context->checkBuffersAndListen();
     context->updateCalendar(step);
     context->sendUpdateCalendar(step);
Index: /XIOS/trunk/src/interface/c/icdata.cpp
===================================================================
--- /XIOS/trunk/src/interface/c/icdata.cpp	(revision 596)
+++ /XIOS/trunk/src/interface/c/icdata.cpp	(revision 597)
@@ -373,5 +373,5 @@
       CTimer::get("XIOS send field").resume();
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
       CArray<double, 1> data(data_k8, shape(data_Xsize), neverDeleteData);
       CField::get(fieldid_str)->setData(data);
@@ -389,5 +389,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 1> data(data_k8, shape(data_Xsize), neverDeleteData);
@@ -407,5 +407,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 2>data(data_k8, shape(data_Xsize, data_Ysize), neverDeleteData);
@@ -425,5 +425,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 3>data(data_k8, shape(data_Xsize, data_Ysize, data_Zsize), neverDeleteData);
@@ -442,5 +442,5 @@
       CTimer::get("XIOS send field").resume();
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<float, 1> data_tmp(data_k4, shape(data_Xsize), neverDeleteData);
@@ -461,5 +461,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<float, 1> data_tmp(data_k4, shape(data_Xsize), neverDeleteData);
@@ -481,5 +481,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<float, 2> data_tmp(data_k4, shape(data_Xsize, data_Ysize), neverDeleteData);
@@ -501,5 +501,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<float, 3> data_tmp(data_k4, shape(data_Xsize, data_Ysize, data_Zsize), neverDeleteData);
@@ -523,5 +523,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 1> data(data_k8, shape(data_Xsize), neverDeleteData);
@@ -541,5 +541,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 2>data(data_k8, shape(data_Xsize, data_Ysize), neverDeleteData);
@@ -559,5 +559,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 3>data(data_k8, shape(data_Xsize, data_Ysize, data_Zsize), neverDeleteData);
@@ -577,5 +577,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 1> data(data_Xsize);
@@ -597,5 +597,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 2> data(data_Xsize, data_Ysize);
@@ -617,5 +617,5 @@
 
       CContext* context = CContext::getCurrent();
-      if (!context->hasServer) context->client->checkBuffers();
+      if (!context->hasServer) context->checkBuffersAndListen();
 
       CArray<double, 3> data(data_Xsize, data_Ysize, data_Zsize);
Index: /XIOS/trunk/src/node/context.cpp
===================================================================
--- /XIOS/trunk/src/node/context.cpp	(revision 596)
+++ /XIOS/trunk/src/node/context.cpp	(revision 597)
@@ -23,16 +23,18 @@
    CContext::CContext(void)
       : CObjectTemplate<CContext>(), CContextAttributes()
-      , calendar(),hasClient(false),hasServer(false), isPostProcessed(false), dataSize_(), idServer_()
+      , calendar(), hasClient(false), hasServer(false), isPostProcessed(false), finalized(false)
+      , dataSize_(), idServer_(), client(0), server(0)
    { /* Ne rien faire de plus */ }
 
    CContext::CContext(const StdString & id)
       : CObjectTemplate<CContext>(id), CContextAttributes()
-      , calendar(),hasClient(false),hasServer(false), isPostProcessed(false), dataSize_(), idServer_()
+      , calendar(), hasClient(false), hasServer(false), isPostProcessed(false), finalized(false)
+      , dataSize_(), idServer_(), client(0), server(0)
    { /* Ne rien faire de plus */ }
 
    CContext::~CContext(void)
    {
-     if (hasClient) delete client;
-     if (hasServer) delete server;
+     delete client;
+     delete server;
    }
 
@@ -231,8 +233,20 @@
 
    //! Initialize client side
-   void CContext::initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer)
+   void CContext::initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer /*= 0*/)
    {
      hasClient=true;
      client = new CContextClient(this,intraComm, interComm, cxtServer);
+     MPI_Comm intraCommServer, interCommServer;
+     if (cxtServer) // Attached mode
+     {
+       intraCommServer = intraComm;
+       interCommServer = interComm;
+     }
+     else
+     {
+       MPI_Comm_dup(intraComm, &intraCommServer);
+       MPI_Comm_dup(interComm, &interCommServer);
+     }
+     server = new CContextServer(this,intraCommServer,interCommServer);
    }
 
@@ -277,8 +291,25 @@
 
    //! Initialize server
-   void CContext::initServer(MPI_Comm intraComm,MPI_Comm interComm)
+   void CContext::initServer(MPI_Comm intraComm,MPI_Comm interComm, CContext* cxtClient /*= 0*/)
    {
      hasServer=true;
      server = new CContextServer(this,intraComm,interComm);
+     MPI_Comm intraCommClient, interCommClient;
+     if (cxtClient) // Attached mode
+     {
+       intraCommClient = intraComm;
+       interCommClient = interComm;
+     }
+     else
+     {
+       MPI_Comm_dup(intraComm, &intraCommClient);
+       MPI_Comm_dup(interComm, &interCommClient);
+     }
+     client = new CContextClient(this,intraCommClient,interCommClient, cxtClient);
+     // Do something clever instead
+     std::map<int, StdSize> bufferSize;
+     for (int r = 0; r < client->serverSize; r++)
+       bufferSize[r] = 10 * sizeof(size_t) * 1024;
+     client->setBufferSize(bufferSize);
    }
 
@@ -289,14 +320,28 @@
    }
 
+   //! Try to send the buffers and receive possible answers
+   bool CContext::checkBuffersAndListen(void)
+   {
+     client->checkBuffers();
+     return server->eventLoop();
+   }
+
    //! Terminate a context
    void CContext::finalize(void)
    {
-      if (hasClient && !hasServer)
-      {
-         client->finalize();
-      }
-      if (hasServer)
-      {
-        closeAllFile();
+      if (!finalized)
+      {
+        finalized = true;
+
+        client->finalize();
+        while (!server->hasFinished())
+        {
+          server->eventLoop();
+        }
+
+        if (hasServer)
+        {
+          closeAllFile();
+        }
       }
    }
Index: /XIOS/trunk/src/node/context.hpp
===================================================================
--- /XIOS/trunk/src/node/context.hpp	(revision 596)
+++ /XIOS/trunk/src/node/context.hpp	(revision 597)
@@ -87,12 +87,12 @@
       public :
          // Initialize server or client
-         void initServer(MPI_Comm intraComm, MPI_Comm interComm);
-         void initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer=0);
+         void initServer(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtClient = 0);
+         void initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer = 0);
          bool isInitialized(void);
 
          // Put sever or client into loop state
          bool eventLoop(void);
-         bool serverLoop(void);
-         void clientLoop(void);
+
+         bool checkBuffersAndListen(void);
 
          // Finalize a context
@@ -210,4 +210,5 @@
       private:
          bool isPostProcessed;
+         bool finalized;
          std::map<int, StdSize> dataSize_;
          StdString idServer_;
Index: /XIOS/trunk/src/server.cpp
===================================================================
--- /XIOS/trunk/src/server.cpp	(revision 596)
+++ /XIOS/trunk/src/server.cpp	(revision 597)
@@ -25,5 +25,5 @@
     bool CServer::finished=false ;
     bool CServer::is_MPI_Initialized ;
-    CEventScheduler* CServer::eventScheduler ;
+    CEventScheduler* CServer::eventScheduler = 0;
 
     void CServer::initialize(void)
@@ -392,5 +392,5 @@
        for(it=contextList.begin();it!=contextList.end();it++)
        {
-         finished=it->second->eventLoop() ;
+         finished=it->second->checkBuffersAndListen();
          if (finished)
          {
