#include <glib.h>
#include <gio/gio.h>
#include <string.h>

int
main ()
{
  GSocketClient *client;
  GSocketConnection *connection;
  GInetAddress *iaddr;
  GSocketAddress *saddr;
  gchar buf[1024];
  gsize bytes_read;
  GError *error = NULL;

  client = g_socket_client_new();

  /* backend server on loopback */
  iaddr = g_inet_address_new_loopback(G_SOCKET_FAMILY_IPV4);
  saddr = g_inet_socket_address_new(iaddr, 10002);
  g_object_unref(iaddr);

  connection = g_socket_client_connect(client, G_SOCKET_CONNECTABLE(saddr), NULL, &error);
  g_object_unref(saddr);

  if (connection == NULL)
    {
      g_printerr("There was a problem connecting: %s",
                 error->message);
      g_error_free(error);
      return -1;
    }

  if (!g_input_stream_read_all(g_io_stream_get_input_stream(G_IO_STREAM(connection)),
                               buf, 12, &bytes_read, NULL, &error))
    {
      g_printerr("There was a problem reading from the socket: %s",
                 error->message);
      g_error_free(error);
      return -1;
    }

  if (!g_input_stream_read_all(g_io_stream_get_input_stream(G_IO_STREAM(connection)),
                               buf, 5, &bytes_read, NULL, &error))
    {
      g_printerr("There was a problem reading from the socket: %s",
                 error->message);
      g_error_free(error);
      return -1;
    }

  return 0;
}
