Index: branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
===================================================================
--- branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 14010)
+++ branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 14013)
@@ -23,5 +23,5 @@
 using System.Collections.Generic;
 using System.IO;
-
+using System.Linq;
 using DistributedGA.Core.Implementation;
 using DistributedGA.Core.Interface;
@@ -127,4 +127,10 @@
     }
 
+    public override void ClearState() {
+      base.ClearState();
+      h.Dispose();
+      h = null;
+    }
+
     private void Init() {
       h = new PeerNetworkMessageHandler();
@@ -141,7 +147,6 @@
       if (h == null) {
         Init();
-        //TODO: sort population
-
       }
+
       if (MigrationIterationsParameter.ActualValue == null) {
         MigrationIterationsParameter.ActualValue = new IntValue(0);
@@ -160,4 +165,13 @@
         }
         var popQualities = QualityParameter.ActualValue;
+        var pop = new List<IScope>(scope.SubScopes);
+
+        List<IScope> sortedPop;
+        if (Maximization.Value) {
+          sortedPop = pop.Zip(popQualities, Tuple.Create).OrderByDescending(t => t.Item2).Select(t => t.Item1).ToList();
+        } else {
+          sortedPop = pop.Zip(popQualities, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList();
+        }
+
         var selectedMigStratSelect = MigrationStrategySelectParameter.Value.Value;
         var selectedMigStratReplace = MigrationStrategyReplaceParameter.Value.Value;
@@ -171,21 +185,17 @@
           switch (selectedMigStratSelect) {
             case MigrationStrategy.Best:
-              emigrants = scope.SubScopes[i];
+              emigrants = sortedPop[i];
               emigrantsList.Add(emigrants);
               break;
 
             case MigrationStrategy.Random:
-              replIdx = rand.Next(scope.SubScopes.Count);
-              emigrants = scope.SubScopes[replIdx];
+              replIdx = rand.Next(sortedPop.Count);
+              emigrants = sortedPop[replIdx];
               emigrantsList.Add(emigrants);
               break;
 
             case MigrationStrategy.Worst:
-              emigrants = scope.SubScopes[scope.SubScopes.Count - i];
+              emigrants = sortedPop[scope.SubScopes.Count - i - 1];
               emigrantsList.Add(emigrants);
-              break;
-
-
-            default:
               break;
           }
@@ -207,5 +217,4 @@
               message = stream.GetBuffer();
               h.PublishDataToNetwork(message);
-
             }
           }
@@ -216,32 +225,34 @@
           // recieve
           var message = h.GetDataFromNetwork();
-          //for (int ei = 0; ei < message.Length; ei++) {
+         
+          // remove individuals from population to make place for immigrants
+          for (int i = 0; i < message.Count; i++) {
+            switch (selectedMigStratReplace) {
+              case MigrationStrategy.Best:
+                scope.SubScopes.Remove(sortedPop[0]);
+                sortedPop.RemoveAt(0);
+                break;
+
+              case MigrationStrategy.Random:
+                replIdx = rand.Next(sortedPop.Count);
+                scope.SubScopes.Remove(sortedPop[replIdx]);
+                sortedPop.RemoveAt(replIdx);
+                break;
+
+              case MigrationStrategy.Worst:
+                //replace random
+                scope.SubScopes.Remove(sortedPop[sortedPop.Count - 1]);
+                sortedPop.RemoveAt(sortedPop.Count - 1);
+                break;
+            }
+          }
+
+          //insert individual sorted in population
+          var qualities = QualityParameter.ActualValue;
+          var qualityTranslatedName = QualityParameter.TranslatedName;
           foreach (var msg in message) {
             using (var stream = new MemoryStream(msg.Value)) {
               var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
 
-              // replace individual in current population
-              switch (selectedMigStratReplace) {
-                case MigrationStrategy.Best:
-                  scope.SubScopes.RemoveAt(0);
-                  break;
-
-                case MigrationStrategy.Random:
-                  replIdx = rand.Next(scope.SubScopes.Count);
-                  scope.SubScopes.RemoveAt(replIdx);
-                  break;
-
-                case MigrationStrategy.Worst:
-                  //replace random
-                  scope.SubScopes.RemoveAt(scope.SubScopes.Count);
-                  break;
-
-                default:
-                  break;
-              }
-
-              //insert individual sortet in population
-              var qualities = QualityParameter.ActualValue;
-              var qualityTranslatedName = QualityParameter.TranslatedName;
               var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
               var insertPos = scope.SubScopes.Count;
@@ -258,6 +269,5 @@
 
               var log = LogParameter.Value;
-              double quality = 0.0;
-              quality = qImmigrant;
+              double quality = qImmigrant;
               log.LogMessage(string.Format("Recieved individual with quality {0} from peer {1}:{2} ; Job: {3}",
                                             quality, msg.Key.IpAddress, msg.Key.Port, msg.Key.ProblemInstance));
