:- dlib_require(socket).
go:-server_create('5000',Acc),
    repeat, 
	s_sleep(3000), % For Test
    all_acc(1,WatchList),
    select_from_sockets(20000000,[Acc|WatchList],ReceivedList),
    member(Sock,ReceivedList),
    rec_do(Acc,Sock,Data),
    write(Sock=Data),nl,
    socket_send(Sock,'Server Data'),
    Data == "QUIT",true;
    fail.   

rec_do(A,A,_):- !,server_accept(A,Host,S),assert('$acc$'(S)),fail.
rec_do(_,S,D):- socket_receive_list(S,D),length(D,N),N>0,!.
rec_do(_,S,_):- socket_close(S),retract('$acc$'(S)),fail.

all_acc(N,[A|L]):- clause('$acc$',1,N,'$acc$'(A)),!,NN is N+1,all_acc(NN,L).
all_acc(_,[]).

