title: Connect to a socket in C++ and Boost Asio in conditional case
body: <p>I have an application which acts like a client which uses Boost Asio in order to connect to a server.
  It works good, but i'd like to change its behavior.</p>
  
  <p>At the moment, when i start it, the application immediately try to connect to the server and if this not happens (because the server is down) then the application doesn't start.</p>
  
  <p>I'd like to use a conditional case in order to decide at runtime when the application have to start the connection with the server: in this way, i can use the other application features whitout having to connect to the server.</p>
  
  <p>I tryed to fill in a conditional case the code which creates the io_obejct and try to connect to a socket but it doesn't work:</p>
  
  <pre><code>if (mode == A_CONDITION) {
      using boost::asio::ip::tcp;
      boost::asio::io_service io_service;
  
      tcp::resolver resolver(io_service);
  
      tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast&lt;string&gt;(porta));
  
      tcp::resolver::iterator iterator = resolver.resolve(query);
  
      tcp::socket s(io_service);
      s.connect(*iterator);   
  }
  </code></pre>
  
  <p>because since "s" is defined in the if statement, it is out of scope in the rest of the code so i doesn't compile:</p>
  
  <blockquote>
    <p>In function <code>'int main(int, char**)'</code>:<br>
    /home/user/src/main.cpp:960:26: error: <code>'s'</code> was not declared in this scope<br>
    /home/user/src/main.cpp:972:60: error: <code>'s'</code> was not declared in this scope<br>
    /home/user/src/main.cpp:1080:60: error: <code>'s'</code> was not declared in this scope</p>
  </blockquote>
  
  <p>This happens because i use three times</p>
  
  <pre><code>boost::asio::write(s, boost::asio::buffer(heading_data, heading_length));
  </code></pre>
  
  <p>outer of the if conditional block.</p>
  
  <p>I can't include the writing on the socket in the conditional case block so i think i have to use a global definition of 's' but i don't know how to do this or if there is another better solution.</p>
  
  <p>I hope you can help me :)</p>
  
  <p>EDIT for pure cuteness:
  If i do this:</p>
  
  <pre><code> tcp::socket *s = 0;
  
   boost::asio::io_service io_service;
  
   tcp::resolver resolver(io_service);
  
   tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast&lt;string&gt;(porta));
  
   tcp::resolver::iterator iterator = resolver.resolve(query);
  
  
   if (mode = FOLLOW_MODE){ 
   s = new tcp::socket(io_service);
   s-&gt;connect(*iterator);  
   }
  </code></pre>
  
  <p>It gives me these errors during the compilation:</p>
  
  <pre><code>&gt; In file included from
  &gt; /usr/include/boost/asio/write.hpp:526:0,
  &gt;                  from /usr/include/boost/asio/buffered_write_stream.hpp:32,
  &gt;                  from /usr/include/boost/asio/buffered_stream.hpp:26,
  &gt;                  from /usr/include/boost/asio.hpp:34,
  &gt;                  from /home/user/src/main.cpp:9:
  &gt; /usr/include/boost/asio/impl/write.ipp:
  &gt; In function 'size_t
  &gt; boost::asio::write(SyncWriteStream&amp;,
  &gt; const ConstBufferSequence&amp;,
  &gt; CompletionCondition,
  &gt; boost::system::error_code&amp;) [with
  &gt; SyncWriteStream =
  &gt; boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;*, ConstBufferSequence =
  &gt; boost::asio::const_buffers_1,
  &gt; CompletionCondition =
  &gt; boost::asio::detail::transfer_all_t,
  &gt; size_t = unsigned int]':
  &gt; /usr/include/boost/asio/impl/write.ipp:57:71:
  &gt; instantiated from 'size_t
  &gt; boost::asio::write(SyncWriteStream&amp;,
  &gt; const ConstBufferSequence&amp;) [with
  &gt; SyncWriteStream =
  &gt; boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;*, ConstBufferSequence =
  &gt; boost::asio::const_buffers_1, size_t =
  &gt; unsigned int]'
  &gt; /home/user/src/main.cpp:980:78:  
  &gt; instantiated from here
  &gt; /usr/include/boost/asio/impl/write.ipp:44:57:
  &gt; error: request for member 'write_some'
  &gt; in 's', which is of non-class type
  &gt; 'boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;*' make[2]: ***
  &gt; [src/CMakeFiles/main.dir/main.cpp.o]
  &gt; Error 1 make[1]: ***
  &gt; [src/CMakeFiles/main.dir/all] Error 2
  &gt; make: *** [all] Error 2
  </code></pre>