Index: code/branches/presentation2/src/libraries/network/CMakeLists.txt
===================================================================
--- code/branches/presentation2/src/libraries/network/CMakeLists.txt	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/CMakeLists.txt	(revision 7793)
@@ -32,4 +32,5 @@
   LANDiscoverable.cc
   LANDiscovery.cc
+  WANDiscoverable.cc
   WANDiscovery.cc
   MasterServerComm.cc
@@ -59,4 +60,5 @@
   LANDiscoverable.h
   LANDiscovery.h
+  WANDiscoverable.h
   WANDiscovery.h
   MasterServerComm.h
Index: code/branches/presentation2/src/libraries/network/Connection.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/Connection.cc	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/Connection.cc	(revision 7793)
@@ -112,5 +112,4 @@
   void Connection::communicationThread()
   {
-    COUT(0) << "starting communication thread" << endl;
     ENetEvent event;
     
Index: code/branches/presentation2/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/MasterServerComm.cc	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/MasterServerComm.cc	(revision 7793)
@@ -48,5 +48,5 @@
 
     /* initialize the event holder */
-    this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
+//     this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
     
 
@@ -90,6 +90,6 @@
 
     /* Wait up to 2 seconds for the connection attempt to succeed. */
-    if (enet_host_service (this->client, this->event, 500) > 0 &&
-        this->event->type == ENET_EVENT_TYPE_CONNECT )
+    if (enet_host_service (this->client, &this->event, 500) > 0 &&
+        this->event.type == ENET_EVENT_TYPE_CONNECT )
       COUT(3) << "Connection to master server succeeded.\n";
     else
@@ -103,7 +103,14 @@
     return 0;
   }
+  
+void MasterServerComm::update()
+{
+  while( enet_host_service( this->client, &this->event, 1 ) );
+}
+
 
   int MasterServerComm::disconnect( void )
   {
+    while( enet_host_service( this->client, &this->event, 1 ) );
     enet_peer_disconnect( this->peer, 0 );
 
@@ -111,10 +118,10 @@
      * and drop any packets received packets.
      */
-    while (enet_host_service (this->client, this->event, 1000) > 0)
+    while (enet_host_service (this->client, &this->event, 1000) > 0)
     {
-      switch (this->event->type)
+      switch (this->event.type)
       {
         case ENET_EVENT_TYPE_RECEIVE:
-          enet_packet_destroy (event->packet);
+          enet_packet_destroy (event.packet);
           break;
 
@@ -154,8 +161,8 @@
     /* enet_host_service returns 0 if no event occured */
     /* just newly set below test to >0 from >= 0, to be tested */
-    if( enet_host_service( this->client, this->event, delayms ) > 0 )
+    if( enet_host_service( this->client, &this->event, delayms ) > 0 )
     { 
       /* check what type of event it is and react accordingly */
-      switch (this->event->type)
+      switch (this->event.type)
       { /* new connection, not supposed to happen. */
         case ENET_EVENT_TYPE_CONNECT: break;
@@ -173,21 +180,21 @@
 
           /* resolve IP */
-          enet_address_get_host_ip( &(this->event->peer->address), 
+          enet_address_get_host_ip( &(this->event.peer->address), 
             addrconv, 49 );
 
           /* DEBUG */
           COUT(3) << "MasterServer Debug: A packet of length " 
-            << this->event->packet->dataLength 
-            << " containing " << this->event->packet->data
+            << this->event.packet->dataLength 
+            << " containing " << this->event.packet->data
             << " was received from " << addrconv 
-            << " on channel " << this->event->channelID;
+            << " on channel " << this->event.channelID;
           /* END DEBUG */
 
           /* call the supplied callback, if any. */
           if( (*callback) != NULL )
-            retval = (*callback)( addrconv, (this->event) );
+            retval = (*callback)( addrconv, &(this->event) );
 
           /* clean up */
-          enet_packet_destroy( event->packet );
+          enet_packet_destroy( event.packet );
           if( addrconv ) 
             free( addrconv );
@@ -220,5 +227,6 @@
    
     /* free the packet */
-    enet_packet_destroy( packet );
+    // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
+//     enet_packet_destroy( packet );
 
     /* all done. */
@@ -239,5 +247,6 @@
     /* One could just use enet_host_service() instead. */
     enet_host_flush( this->client );
-    enet_packet_destroy( packet );
+    // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
+//     enet_packet_destroy( packet );
 
     /* all done. */
Index: code/branches/presentation2/src/libraries/network/MasterServerComm.h
===================================================================
--- code/branches/presentation2/src/libraries/network/MasterServerComm.h	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/MasterServerComm.h	(revision 7793)
@@ -53,4 +53,6 @@
        */
       int initialize();
+      
+      void update();
 
 
@@ -92,8 +94,8 @@
     private:
       /** client handle */
-      ENetHost *client;
+      ENetHost* client;
 
       /** event data holder */
-      ENetEvent *event;
+      ENetEvent event;
 
       /** address holder */
@@ -101,5 +103,5 @@
 
       /** peer data holder */
-      ENetPeer *peer;
+      ENetPeer* peer;
   }; 
 
Index: code/branches/presentation2/src/libraries/network/Server.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/Server.cc	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/Server.cc	(revision 7793)
@@ -104,6 +104,6 @@
   void Server::helper_ConnectToMasterserver()
   {
-    WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 
-      MSPROTO_REGISTER_SERVER );
+//     WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 
+//       MSPROTO_REGISTER_SERVER );
   }
 
@@ -121,8 +121,9 @@
 
     /* make discoverable on WAN */
+    WANDiscoverable::setActivity(true);
     /* TODO this needs to be optional, we need a switch from the UI to
      * enable/disable this 
      */
-    helper_ConnectToMasterserver();
+//     helper_ConnectToMasterserver();
 
     /* done */
@@ -142,6 +143,5 @@
     /* tell master server we're closing */
     COUT(2) << "disconnecting." << endl;
-    WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 
-      MSPROTO_SERVERDC );
+    WANDiscoverable::setActivity(false);    
     COUT(2) << "disconnecting done" << endl;
 
@@ -202,5 +202,5 @@
     // receive and process incoming discovery packets
     LANDiscoverable::update();
-
+    
     // receive and process requests from master server
     /* todo */
Index: code/branches/presentation2/src/libraries/network/Server.h
===================================================================
--- code/branches/presentation2/src/libraries/network/Server.h	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/Server.h	(revision 7793)
@@ -40,6 +40,7 @@
 #include "ServerConnection.h"
 #include "LANDiscoverable.h"
-#include "MasterServerComm.h"
-#include "MasterServerProtocol.h"
+#include "WANDiscoverable.h"
+// #include "MasterServerComm.h"
+// #include "MasterServerProtocol.h"
 
 
@@ -51,5 +52,6 @@
   * It implements all functions necessary for a Server
   */
-  class _NetworkExport Server : public Host, public ServerConnection, public LANDiscoverable{
+  class _NetworkExport Server : public Host, public ServerConnection, public LANDiscoverable, public WANDiscoverable
+  {
   public:
     Server();
Index: code/branches/presentation2/src/libraries/network/WANDiscoverable.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/WANDiscoverable.cc	(revision 7793)
+++ code/branches/presentation2/src/libraries/network/WANDiscoverable.cc	(revision 7793)
@@ -0,0 +1,124 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau (original)
+ *   Co-authors:
+ *      Sandro 'smerkli' Merkli (adaptions to WAN)
+ *      ...
+ *
+ */
+
+#include "WANDiscoverable.h"
+
+#include <enet/enet.h>
+#include <cstring>
+
+#include "MasterServerProtocol.h"
+#include "core/ConfigValueIncludes.h"
+#include "core/CoreIncludes.h"
+
+
+namespace orxonox
+{
+
+  WANDiscoverable::WANDiscoverable(): bActive_(false)
+  {
+    /* debugging output */
+    COUT(4) << "Creating WANDiscoverable.\n";
+  
+    /* register object in orxonox */
+    RegisterObject(WANDiscoverable);
+
+    /* check for the masterserver address option in orxonox.ini */
+    this->setConfigValues();
+    
+  }
+
+  void WANDiscoverable::setConfigValues()
+  {
+    /* update msaddress string from orxonox.ini config file, if it 
+     * has changed. 
+     */
+    SetConfigValueExternal(msaddress, "WANDiscovery", "msaddress", "orxonox.net");
+//     SetConfigValue( msaddress, "orxonox.net");
+  } 
+
+  WANDiscoverable::~WANDiscoverable()
+  {
+    if( this->bActive_ )
+      this->disconnect();
+  }
+  
+  void WANDiscoverable::setActivity(bool bActive)
+  {
+    if( bActive==this->bActive_ )
+      return;
+    
+    if( bActive )
+    {
+      if( this->connect() )
+        this->bActive_ = true;
+    }
+    else
+    {
+      this->disconnect();
+      this->bActive_ = false;
+    }
+  }
+  
+  bool WANDiscoverable::connect()
+  {
+    /* initialize it and see if it worked */
+    if( msc.initialize() )
+    {
+      COUT(2) << "Error: could not initialize master server communications!\n";
+      return false;
+    }
+    
+    /* connect and see if it worked */
+    if( msc.connect( this->msaddress.c_str(), ORX_MSERVER_PORT ) )
+    {
+      COUT(2) << "Error: could not connect to master server at " 
+                 << this->msaddress << std::endl;
+      return false;
+    }
+                 
+    /* debugging output */
+    COUT(4) << "Initialization of WANDiscoverable complete.\n";
+    
+    
+    // Now register the server at the master server
+    this->msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_REGISTER_SERVER );
+    
+    return true;
+  }
+
+  void WANDiscoverable::disconnect()
+  {
+    this->msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_SERVERDC );
+    msc.disconnect();
+  }
+
+
+
+  
+} // namespace orxonox
Index: code/branches/presentation2/src/libraries/network/WANDiscoverable.h
===================================================================
--- code/branches/presentation2/src/libraries/network/WANDiscoverable.h	(revision 7793)
+++ code/branches/presentation2/src/libraries/network/WANDiscoverable.h	(revision 7793)
@@ -0,0 +1,77 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or modify it
+ *   under the terms of the GNU General Public License as published by the Free
+ *   Software Foundation; either version 2 of the License, or (at your option)
+ *   any later version.
+ *
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ *   for more details.
+ *
+ *   You should have received a copy of the GNU General Public License along
+ *   with this program; if not, write to the Free Software Foundation, Inc., 51
+ *   Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author: Fabian 'x3n' Landau (original) Co-authors: Sandro 'smerkli' Merkli
+ *   (copied and adapted to WAN)
+ *
+ */
+
+#ifndef _WANDiscoverable_H__
+#define _WANDiscoverable_H__
+
+#include "NetworkPrereqs.h"
+// #include "core/OrxonoxClass.h"
+#include "core/CoreIncludes.h"
+#include "MasterServerComm.h"
+
+namespace orxonox
+{
+
+  class _NetworkExport WANDiscoverable: public OrxonoxClass
+  {
+    public:
+      /** constructor */
+      WANDiscoverable();
+
+      /** destructor */
+      ~WANDiscoverable();
+
+      /** \return Address of the master server
+       * 
+       * Get the master server address 
+       */
+      std::string getMSAddress()
+      { return this->msaddress; }
+
+      /** Function used for the configuration file parameter update */
+      void setConfigValues();
+      
+      /** Function used to set the activity/discoverability */
+      void setActivity( bool bActive );
+
+      /** Master server communications object */
+      MasterServerComm msc;
+      
+    private:
+      /** Function used to connect to the master server */
+      bool connect();
+      
+      /** Function used to disconnect from the master server */
+      void disconnect();
+      
+      /** master server address */
+      std::string msaddress;
+      bool        bActive_;
+
+  };
+
+}
+
+#endif // _WANDiscoverable_H__
Index: code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc	(revision 7793)
@@ -21,5 +21,5 @@
  *
  *   Author:
- *      Fabian 'x3n' Landau
+ *      Oliver Scheuss
  *   Co-authors:
  *      ...
@@ -60,5 +60,6 @@
 
       /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
-      //assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
+      // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
+//       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
 
       // Save Server Name
Index: code/branches/presentation2/src/libraries/network/packet/ServerInformation.h
===================================================================
--- code/branches/presentation2/src/libraries/network/packet/ServerInformation.h	(revision 7790)
+++ code/branches/presentation2/src/libraries/network/packet/ServerInformation.h	(revision 7793)
@@ -21,5 +21,5 @@
  *
  *   Author:
- *      Fabian 'x3n' Landau
+ *      Oliver Scheuss
  *   Co-authors:
  *      ...
