Index: branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs
===================================================================
--- branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs	(revision 15697)
+++ branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/Query/QueryClient.cs	(revision 16692)
@@ -1,5 +1,5 @@
 ﻿#region License Information
 /* HeuristicLab
- * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
  *
  * This file is part of HeuristicLab.
@@ -22,8 +22,11 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.ServiceModel;
 using HeuristicLab.Clients.Common;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
+using HeuristicLab.Persistence.Default.Xml;
 
 namespace HeuristicLab.Clients.OKB.Query {
@@ -38,4 +41,7 @@
     }
 
+    private int endpointRetries;
+    private string workingEndpoint;
+
     #region Properties
     private List<Filter> filters;
@@ -61,20 +67,18 @@
         filters.AddRange(CallQueryService<List<Filter>>(s => s.GetFilters()));
         valueNames.AddRange(CallQueryService<List<ValueName>>(s => s.GetValueNames()));
-      }
-      finally {
+      } finally {
         OnRefreshed();
       }
     }
     public void RefreshAsync(Action<Exception> exceptionCallback) {
-      var call = new Func<Exception>(delegate() {
+      var call = new Func<Exception>(delegate () {
         try {
           Refresh();
-        }
-        catch (Exception ex) {
+        } catch (Exception ex) {
           return ex;
         }
         return null;
       });
-      call.BeginInvoke(delegate(IAsyncResult result) {
+      call.BeginInvoke(delegate (IAsyncResult result) {
         Exception ex = call.EndInvoke(result);
         if (ex != null) exceptionCallback(ex);
@@ -95,4 +99,48 @@
     public IEnumerable<Run> GetRunsWithValues(IEnumerable<long> ids, bool includeBinaryValues, IEnumerable<ValueName> vn) {
       return CallQueryService<IEnumerable<Run>>(s => s.GetRunsWithValues(ids.ToList(), includeBinaryValues, vn.ToList()));
+    }
+    #endregion
+
+    #region OKB-Item Conversion
+    public Optimization.IRun ConvertToOptimizationRun(Run run) {
+      Optimization.Run optRun = new Optimization.Run();
+      foreach (Value value in run.ParameterValues)
+        optRun.Parameters.Add(value.Name, ConvertToItem(value));
+      foreach (Value value in run.ResultValues)
+        optRun.Results.Add(value.Name, ConvertToItem(value));
+      return optRun;
+    }
+
+    public IItem ConvertToItem(Value value) {
+      if (value is BinaryValue) {
+        IItem item = null;
+        var binaryValue = (BinaryValue)value;
+        if (binaryValue.Value != null) {
+          using (var stream = new MemoryStream(binaryValue.Value)) {
+            try {
+              item = XmlParser.Deserialize<IItem>(stream);
+            } catch (Exception) { }
+            stream.Close();
+          }
+        }
+        return item ?? new Data.StringValue(value.DataType.Name);
+      } else if (value is BoolValue) {
+        return new Data.BoolValue(((BoolValue)value).Value);
+      } else if (value is FloatValue) {
+        return new Data.DoubleValue(((FloatValue)value).Value);
+      } else if (value is PercentValue) {
+        return new Data.PercentValue(((PercentValue)value).Value);
+      } else if (value is DoubleValue) {
+        return new Data.DoubleValue(((DoubleValue)value).Value);
+      } else if (value is IntValue) {
+        return new Data.IntValue((int)((IntValue)value).Value);
+      } else if (value is LongValue) {
+        return new Data.IntValue((int)((LongValue)value).Value);
+      } else if (value is StringValue) {
+        return new Data.StringValue(((StringValue)value).Value);
+      } else if (value is TimeSpanValue) {
+        return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
+      }
+      return null;
     }
     #endregion
@@ -112,14 +160,40 @@
 
     #region Helpers
+    private QueryServiceClient NewServiceClient() {
+      if (endpointRetries >= Properties.Settings.Default.MaxEndpointRetries)
+        return CreateClient(workingEndpoint);
+
+      var configurations = Properties.Settings.Default.EndpointConfigurationPriorities;
+      Exception exception = null;
+
+      foreach (var endpointConfigurationName in configurations) {
+        try {
+          var cl = CreateClient(endpointConfigurationName);
+          cl.Open();
+          workingEndpoint = endpointConfigurationName;
+          return cl;
+        } catch (EndpointNotFoundException e) {
+          exception = e;
+          ++endpointRetries;
+        }
+      }
+
+      throw exception ?? new EndpointNotFoundException("No endpoint for Query service found.");
+    }
+
+    private QueryServiceClient CreateClient(string endpointConfigurationName) {
+      var cl = ClientFactory.CreateClient<QueryServiceClient, IQueryService>(endpointConfigurationName);
+      return cl;
+    }
+
     private T CallQueryService<T>(Func<IQueryService, T> call) {
-      QueryServiceClient client = ClientFactory.CreateClient<QueryServiceClient, IQueryService>();
+      QueryServiceClient client = NewServiceClient();
+
       try {
         return call(client);
-      }
-      finally {
+      } finally {
         try {
           client.Close();
-        }
-        catch (Exception) {
+        } catch (Exception) {
           client.Abort();
         }
