#include <tos.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iconnect\socket.h>						/* IConnect	*/
#include <iconnect\sockinit.h>					/* IConnect	*/
#include <iconnect\in.h>								/* IConnect	*/
#include <iconnect\netdb.h>							/* IConnect	*/
#include <iconnect\sfcntl.h>						/* IConnect	*/
#include <iconnect\atarierr.h>
#include <sting\transprt.h>							/* STinG	*/
#include <windom.h>
#include "gfx.h"
#include "atari.h"
#include "screens.h"
#include "ami2atar.h"
#include "struct\SysStr.h"



long get_sting_cookie (void);

extern struct AtariSystem *UserSystemPtr;
extern struct AtariSystem UserSystem;


DRV_LIST  *sting_drivers;
TPL       *tpl;
unsigned long ip_rk, handle_STiK;


/* SPRAWDZIC DZIALANIE TEJ FUNKCJI !!!!!!	*/

hostent	*gethostbyname_TOT(const char *name)
	{
	hostent *exit=NULL;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = gethostbyname(name);
		}
	else
		{
		if(UserSystemPtr->TCPIP_ID	== TCPID_STING && tpl && sting_drivers)
			{
			exit = AllocMem(sizeof(hostent), MEMF_CLEAR|MEMF_STRAM);
			exit->h_aliases = (char**)KRmalloc(16);

			exit->h_addr_list = KRmalloc(16);
			exit->h_length = resolve((char*)name, 0, &ip_rk, 1);			/* 1 -> 4	*/
			exit->h_name = (char*)name;
			exit->h_addrtype = AF_INET;

			if(exit->h_length<=0)
				{
				KRfree(exit->h_aliases);
				KRfree(exit->h_addr_list);
				FreeVec(exit);
				exit = NULL;
				}
			}
		}
	return(exit);
	}


int connect_TOT(int s, const void *addr, ULONG addrlen)
	{
	int exit;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = connect(s, addr, (int)addrlen);
		}
	else
		{
		if(addr)
			{
			handle_STiK = TCP_open((uint32)ip_rk ,(uint16)((sockaddr_in*)addr)->sin_port, (uint16)0, 10000);	/*(uint16)addrlen);*/
			if(handle_STiK>=0)			/* Success ?	*/
				{
				exit = E_OK;
				}
			}
		else
			{
			exit = ERROR;
			}
		}
	return(exit);
	}



int sclose_TOT(int s)
	{
	int exit;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = sclose(s);
		}
	else
		{
		exit = TCP_close((int16)handle_STiK,-1,NULL);
		}
	return(exit);
	}



long recv_TOT(int s, void *buf, long len, int flags)
	{
	long exit, len1;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = recv(s, buf, len, flags);
		}
	else
		{

		if ((len1 = CNbyte_count((int16)handle_STiK)) > 0)
			{
			exit = CNget_block((int16)handle_STiK, (void*)buf, (int16)len1);
			}
    else
    	{
    	if (len1 < -2)
    		{
    		Raport("receiving error #%d : %s. Ilosc bajtow do odebrania %ld", exit, get_err_text(exit), len1);
    		return(0);
    		}
    	} /*  All the serious error codes are below -2 */
		}
	return(exit);
	}


int send_TOT(int s, const void *msg, ULONG len, int flags)
	{
	int exit, nr;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = send(s, msg, (int)len, flags);			/* Ilosc bajtow wyslanych	*/
		}
	else
		{

		for(nr=0; nr<50; nr++)
			{
	/*		evnt_timer(100, 0);		*/
			exit = TCP_send((int16)handle_STiK, (void*)msg, (int16)len);					/* E_NORMAL gdy udane, inaczej numer bledu	*/
			if(exit==E_NORMAL)		/* exit=E_OBUFFULL	-> przepelniony bufor wyjscia	*/
				{
				break;
				}
			}																																			/* lub E_OBUFFULL gdy bufor wyjsciowy jest przepelniony	*/
			
		if(exit==E_NORMAL)
			{
			exit = (int)len;
			}
		else
			{
			Raport("sending error #%d : %s", exit, get_err_text(exit));
			exit = ENHNDL;
			}
		}
	return(exit);
	}



int socket_TOT(int af, int type, int protocol)
	{
	int exit;

	if(UserSystemPtr->TCPIP_ID	== TCPID_ICONNECT)
		{
		exit = socket(af, type, protocol);
		}
	else
		{
		exit = 1;
	/*	exit = TCP_open(ip_rk, type, 0, 1000);	*/
		}
	return(exit);
	}