#ifndef SOCKET_DEF
 #define SOCKET_DEF


//------------------------- SOCKET Module -----------------------//
//						  v 05 . 03 .  2007						 //
//																 //
//								MT								 //
//---------------------------------------------------------------//


//---------------------- Incudes 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

#include "../../ui/onerror.hpp"
//====================== Includes


//---------------------- Defines
#define BUF_NOT_COPY 0
#define BUF_COPY 1
//====================== Defines




// ---------------- CSocket -------------- //
 class CSocket {
 	int socketfd;
	struct sockaddr_in addr;
	
	void bufalloc(int new_size, char copy = BUF_COPY);
	void buffree();

	char *buf;						// socket message
	int buf_size;					// socket message size

 public: 

	CSocket () { buf = new char[1]; buf_size = 1; }; 
	~CSocket () { buffree(); };	
	
	void ConnectTo(char *host, int portno);
	void CloseHost();

	void Read(int size);
	void ReadNext(int size);

	int  ReadAfterWait_Int(char *s, int space,int length);
	int  ReadAfterWait2_Int(char *s, char *s2, int space, int length);
	int  Read_Int();
	int  Read_Str();

	void Write(char *s);
	void SendCommand(char *s);
	void SendCommandWithInt(char *s, int i);
	void SendCommandWithInt2(char *s, int i, int i2);
	int Wait(char *s);				// return -1, if bad waiting
	int Wait2(char *s, char *s2);	// wait OR

	char* GetBufAddr();
	int GetBufSize();
	void DelSpacesInBuf();
	// --- Variables ---
	CError error;
	
 };


#endif
