#include <iostream>
#include <string>
#include <asio.hpp>

using asio::ip::tcp;

int main() {
  try {
    asio::io_service io_service;
    tcp::endpoint endpoint(tcp::v4(), 4001);
    tcp::acceptor acceptor(io_service, endpoint);

    for (;;) {
      tcp::iostream stream;
      acceptor.accept(*stream.rdbuf()); // wait for the new connection
	  while(stream.good()) {
		std::string s;
		std::getline(stream,s); // read data from the client (lose a \n)
		stream << s << std::endl; // send it back with a new \n
	  }
    }
  }
  catch (std::exception& e) {
    std::cerr << e.what() << std::endl;
  }
  return 0;
}
