{ Socket socket = null;
{
   private void connectToStateProvider(StateHeader hdr)   {      Socket socket = null;      IpAddress address = hdr.bind_addr;      String tmp_state_id = hdr.getStateId();      StreamingInputStreamWrapper wrapper = null;      StateTransferInfo sti = null;      try      {         socket = new Socket();         socket.bind(new InetSocketAddress(bind_addr, 0));         int bufferSize = socket.getReceiveBufferSize();         socket.setReceiveBufferSize(socket_buffer_size);         if (log.isDebugEnabled())            log.debug("Connecting to state provider " + address.getIpAddress() + ":" + address.getPort()                  + ", original buffer size was " + bufferSize + " and was reset to " + socket.getReceiveBufferSize());         socket.connect(new InetSocketAddress(address.getIpAddress(), address.getPort()));         if (log.isDebugEnabled())            log.debug("Connected to state provider, my end of the socket is " + socket.getLocalAddress() + ":"                  + socket.getLocalPort() + " passing inputstream up...");         //write out our state_id and address so state provider can clear this request         ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());         out.writeObject(tmp_state_id);         out.writeObject(local_addr);         wrapper = new StreamingInputStreamWrapper(socket);         sti = new StateTransferInfo(hdr.sender, wrapper, tmp_state_id);                        }      catch (IOException e)      {         if (warn)         {            log.warn("State reader socket thread spawned abnormaly", e);         }                  //pass null stream up so that JChannel.getState() returns false          InputStream is = null;         sti = new StateTransferInfo(hdr.sender, is, tmp_state_id);               }      finally      {         if (!socket.isConnected())         {            if (warn)               log.warn("Could not connect to state provider. Closing socket...");            try            {               if (wrapper != null)               {                  wrapper.close();               }               else               {                  socket.close();               }            }            catch (IOException e)            {            }            //since socket did not connect properly we have to            //clear our entry in state providers hashmap "manually"            Message m = new Message(hdr.sender);            StateHeader mhdr = new StateHeader(StateHeader.STATE_REMOVE_REQUESTER, local_addr, tmp_state_id);            m.putHeader(NAME, mhdr);            passDown(new Event(Event.MSG, m));         }         passStreamUp(sti);      }   }