Index: /trunk/FACT++/src/RemoteControl.h
===================================================================
--- /trunk/FACT++/src/RemoteControl.h	(revision 14545)
+++ /trunk/FACT++/src/RemoteControl.h	(revision 14546)
@@ -175,4 +175,5 @@
     }
 
+    /*
     void JsSleep(uint32_t ms)
     {
@@ -185,5 +186,5 @@
 
         T::Unlock();
-    }
+    }*/
 
     int JsWait(const string &server, int32_t state, uint32_t ms)
@@ -237,31 +238,51 @@
     }
 
+    struct EventInfo
+    {
+        EventImp *ptr;
+        uint64_t counter;
+        Event data;
+        EventInfo(EventImp *p) : ptr(p), counter(0) { }
+    };
+
     // Keep a copy of the data for access by V8
-    map<string, EventImp *> fEvents;   // For unsibscription
-    map<string, pair<uint64_t, Event>> fData;
-
+    map<string, EventInfo> fInfo;
     std::mutex fMutex;
 
     pair<uint64_t, EventImp *> JsGetEvent(const std::string &service)
     {
+        // This function is called from JavaScript
         const lock_guard<mutex> lock(fMutex);
 
-        const auto it = fData.find(service);
-
-        return it==fData.end() ? make_pair(uint64_t(0), (Event*)0) : make_pair(it->second.first, &it->second.second);
+        const auto it = fInfo.find(service);
+        if (it==fInfo.end())
+            return make_pair(0, static_cast<EventImp*>(NULL));
+
+        EventInfo &info = it->second;
+        return make_pair(info.counter, (EventImp*)&info.data);
     }
 
     int Handle(const EventImp &evt, const string &service)
     {
-        const lock_guard<mutex> lock(fMutex);
-
-        const auto it = fData.find(service);
-        if (it==fData.end())
-            fData[service] = make_pair(0, static_cast<Event>(evt));
-        else
-        {
-            it->second.first++;
-            it->second.second = static_cast<Event>(evt);
-        }
+        // This function is called from the StateMachine
+        fMutex.lock();
+
+        const auto it = fInfo.find(service);
+
+        // This should never happen... but just in case.
+        if (it==fInfo.end())
+        {
+            fMutex.unlock();
+            return StateMachineImp::kSM_KeepState;
+        }
+
+        EventInfo &info = it->second;
+
+        const uint64_t cnt = info.counter++;
+        info.data = static_cast<Event>(evt);
+
+        fMutex.unlock();
+
+        JsHandleEvent(evt, cnt, service);
 
         return StateMachineImp::kSM_KeepState;
@@ -273,9 +294,14 @@
             return 0;
 
+        // This function is called from JavaScript
+        const lock_guard<mutex> lock(fMutex);
+
         // Do not subscribe twice
-        if (fEvents.find(service)!=fEvents.end())
+        if (fInfo.find(service)!=fInfo.end())
             return 0;
 
-        return fEvents[service] = &fImp->Subscribe(service)(fImp->Wrap(bind(&RemoteControl<T>::Handle, this, placeholders::_1, service)));
+        EventImp *ptr = &fImp->Subscribe(service)(fImp->Wrap(bind(&RemoteControl<T>::Handle, this, placeholders::_1, service)));
+        fInfo.insert(make_pair(service, EventInfo(ptr)));
+        return ptr;
     }
 
@@ -285,10 +311,13 @@
             return false;
 
-        const auto it = fEvents.find(service);
-        if (it==fEvents.end())
+        // This function is called from JavaScript
+        const lock_guard<mutex> lock(fMutex);
+
+        const auto it = fInfo.find(service);
+        if (it==fInfo.end())
             return false;
 
-        fImp->Unsubscribe(it->second);
-        fEvents.erase(it);
+        fImp->Unsubscribe(it->second.ptr);
+        fInfo.erase(it);
 
         return true;
@@ -297,7 +326,11 @@
     void UnsubscribeAll()
     {
-        for (auto it=fEvents.begin(); it!=fEvents.end(); it++)
-            fImp->Unsubscribe(it->second);
-        fEvents.clear();
+        // This function is called from JavaScript
+        const lock_guard<mutex> lock(fMutex);
+
+        for (auto it=fInfo.begin(); it!=fInfo.end(); it++)
+            fImp->Unsubscribe(it->second.ptr);
+
+        fInfo.clear();
     }
 
