title: Client Socket connects to multiple sockets and balances load towards a free socket
body: <p>I am being asked to design the following programming problem.</p>
  
  <p>Previously when a client sent a request to my application I was required to route it towards a specific port on another server. The server would response in a timely manner and a response would be sent from their server to my server and I would send the information to my client. This worked fine until the load increased and we realized that the other server did not handle multi threaded calls.</p>
  
  <p>I have been given a limited set of ports by the other app so that whenever a load of client requests enters my application I must balance the load among those ports in such a manner that if port A is not free I send the client request to port B and if B is not free it goes to port C. [I know that this is not a correct solution, management wants it to work that way]</p>
  
  <p>All calls handled are synchronous and all ports (requirement from application X) must always be kept open.</p>
  
  <p>My biggest problems at the moment are:</p>
  
  <ol>
  <li><p>Knowing when a port is free. (not waiting for a response)</p></li>
  <li><p>How to push the load to the other ports.</p></li>
  </ol>
  
  <p>I need some pointers to where to head to. 
  Could you please help?</p>
  
  <p>What I have done so far is load multiple sockets into an array of SocketAddress.</p>
  
  <pre><code>     Selector socketSelector = SelectorProvider.provider().openSelector();
          int interestSet = SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE | SelectionKey.OP_READ;
          List&lt;SocketAddress&gt; socketAddresses = FactorySockectConnection.getSocketInstances();
  
          for (SocketAddress socketAddress : socketAddresses) {
              SocketChannel socket = SocketChannel.open(socketAddress);
              socket.configureBlocking(false);
              socket.register(socketSelector, interestSet);
          }
          System.out.println("Start");
  
          while (true) {
  
              try {
                  socketSelector.select();
  
                  Set&lt;SelectionKey&gt; keys = socketSelector.selectedKeys();
                  Iterator&lt;SelectionKey&gt; keyIterator = keys.iterator();
                  while (keyIterator.hasNext()) {
  
                      SelectionKey selectedKey = keyIterator.next();
  
                      if (selectedKey.isConnectable()) {
                          SocketChannel connectChannel = (SocketChannel) selectedKey.channel();
                          connectChannel.finishConnect();
                      }
                      if (selectedKey.isWritable() &amp;&amp; requestMessageByte != null) {
                          SocketChannel writeChannel = (SocketChannel) selectedKey.channel();
                          ByteBuffer buf = ByteBuffer.wrap(requestMessageByte.getBytes());
                          while (buf.hasRemaining()) {
                              writeChannel.write(buf);
                          }
                          requestMessageByte = null;
                      }
  
                      if (selectedKey.isReadable() &amp;&amp; responseMessage == null) {
                          SocketChannel readChannel = (SocketChannel) selectedKey.channel();
  
                          ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                          readChannel.read(readBuffer);
                          responseMessage = new String(readBuffer.array());
                      }
                      keyIterator.remove();
                  }
  </code></pre>