New update for SocketUnitEx (added: threaded server socket). Enjoy:
Code:
{
  ---------------------------------------------------

  SocketUnitEx.pas v1.3 (Threaded ServerSocket)
	Original unit by Aphex -> unremote@knology.net
	Modified by Wr #!d3 -> Wrh1d3@gmail.com

  ---------------------------------------------------

  As all open source software, you can redistribute,
  modify code, ... But don't forget to give credits :)

  ---------------------------------------------------

  Modified by Wr #!d3 Monday 12th May 2016 3:03:34 PM

  ---------------------------------------------------
}

unit SocketUnitEx; 

interface

uses
  Windows, Winsock, Classes;

type
  TClientSocket = class
  private
		FSocket: TSocket;  
    FData: Pointer;
    FConnected: Boolean;
  public
    constructor Create;
    procedure Connect(Host: string; Port: Word);
    destructor Destroy; override;  
    procedure SetConnection;
    procedure Disconnect;
    procedure SetIdle;
    function LocalAddress: string;
		function RemoteAddress: string;
		function LocalPort: Word;
    function RemotePort: Word;
    function SendBuffer(var Buffer; BufferSize: Integer): Integer;
    function RecvBuffer(var Buffer; BufferSize: Integer): Integer;
    function SendText(const Text: string): Integer;
    function ReceiveText: string;
    property Socket: TSocket read FSocket write FSocket;
    property Data: Pointer read FData write FData;
    property Connected: Boolean read FConnected;
  end;
	
  TClientSocketThread = class(TThread)
  private
    FReceivedText: string;              
		FClientSocket: TClientSocket;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean = True); 
    property ClientSocket: TClientSocket read FClientSocket write FClientSocket;
  end;
  
  TServerSocketThread = class(TThread)
  private
    FSocket: TSocket;
    FPort: Word;
    FListening: Boolean;  
		FClientSocket: TClientSocket;
    FClientSocketThread: TClientSocketThread;
    procedure SetIdle;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean = True);
    procedure StopListening;
    property Port: Word read FPort write FPort;
    property Listening: Boolean read FListening;
  end;
	
implementation

var
	WSAData: TWSAData;

{Region TClientSocket}
//--Begin
constructor TClientSocket.Create;
begin    
	inherited Create;
  FSocket := INVALID_SOCKET;
  FConnected := False;
end;

destructor TClientSocket.Destroy;
begin
  Disconnect;
  inherited Destroy;
end;

//Set connection status without connecting to a server 
procedure TClientSocket.SetConnection;
begin
  if FSocket <> INVALID_SOCKET then FConnected := True else
  Disconnect;
end;

//Set connection status by connecting to server
procedure TClientSocket.Connect(Host: string; Port: Word);
var
  SockAddrIn: TSockAddrIn;
  HostEnt: PHostEnt;
begin
  FSocket := WinSock.socket(AF_INET, SOCK_STREAM, 0); 
  if FSocket = INVALID_SOCKET then Exit; 

  SockAddrIn.sin_family := AF_INET;
	SockAddrIn.sin_port := htons(Port);
  SockAddrIn.sin_addr.S_addr := inet_addr(PChar(Host));

  //Resolving ip address
	if SockAddrIn.sin_addr.s_addr = INADDR_NONE then
  begin
    HostEnt := gethostbyname(PChar(Host));
    if HostEnt = nil then Exit;
    SockAddrIn.sin_addr.s_addr := Longint(PLongint(HostEnt^.h_addr_list^)^);
  end;
	                                   
  if WinSock.connect(FSocket, SockAddrIn, SizeOf(SockAddrIn)) = 0 then FConnected := True;
  //Set FConnected True only if connected
end;

//Clearing initialized variable and close connection
procedure TClientSocket.Disconnect;
begin
  shutdown(FSocket, SD_BOTH);
  closesocket(FSocket);
	FSocket := INVALID_SOCKET;
  FConnected := False;
end;

procedure TClientSocket.SetIdle; 
var
  FDset: TFDset;
  TimeVal: TTimeVal;
begin
  FD_ZERO(FDSet);
  FD_SET(FSocket, FDSet);
  select(0, @FDset, nil, nil, nil);
end;

function TClientSocket.LocalAddress: string;
var
  SockAddrIn: TSockAddrIn;
  Size: integer;
begin
  Size := sizeof(SockAddrIn);
  getsockname(FSocket, SockAddrIn, Size);
  Result := inet_ntoa(SockAddrIn.sin_addr);
end;

function TClientSocket.RemoteAddress: string;
var
  SockAddrIn: TSockAddrIn;
  Size: Integer;
begin
  Size := sizeof(SockAddrIn);
  getpeername(FSocket, SockAddrIn, Size);
  Result := inet_ntoa(SockAddrIn.sin_addr);
end;

function TClientSocket.LocalPort: Word;
var
  SockAddrIn: TSockAddrIn;
  Size: Integer;
begin
  Size := sizeof(SockAddrIn);
  getsockname(FSocket, SockAddrIn, Size);
  Result := ntohs(SockAddrIn.sin_port);
end;

function TClientSocket.RemotePort: Word;
var
  SockAddrIn: TSockAddrIn;
  Size: Integer;
begin
  Size := sizeof(SockAddrIn);
  getpeername(FSocket, SockAddrIn, Size);
  Result := ntohs(SockAddrIn.sin_port);
end;

function TClientSocket.SendBuffer(var Buffer; BufferSize: Integer): Integer;
begin
  Result := send(FSocket, Buffer, BufferSize, 0);
  if Result = -1 then Disconnect; //Disconnect if failed to send some datas
end;

function TClientSocket.RecvBuffer(var Buffer; BufferSize: Integer): Integer;
begin
  if BufferSize = -1 then
  begin
    //ioctlsocket set Result length to received data size
    if ioctlsocket(FSocket, FIONREAD, Longint(Result)) = SOCKET_ERROR then Disconnect;
  end
  else
  begin
    Result := recv(FSocket, Buffer, BufferSize, 0); //Write received datas to Buffer
    if Result <= 0 then Disconnect; //Disconnect if failed to receive datas
  end;
end;

function TClientSocket.SendText(const Text: string): Integer;
begin
  Result := SendBuffer(Pointer(Text)^, Length(Text));
end;
  
function TClientSocket.ReceiveText: string;
begin
  SetLength(Result, RecvBuffer(Pointer(nil)^, -1)); //First set Result length to received data size.
  SetLength(Result, RecvBuffer(Pointer(Result)^, Length(Result)));
end;
//--End;

{Region TClientSocketThread}
//--Begin
constructor TClientSocketThread.Create(CreateSuspended: Boolean);
begin
	inherited Create(True);      
  FreeOnTerminate := True;
  FClientSocket := nil;
  FReceivedText := '';
end;

procedure TClientSocketThread.Execute;
begin
  repeat
    FClientSocket.SetIdle;
		FReceivedText := FClientSocket.ReceiveText;
		//Now do what you want with FReceivedText !
  until False;

	//Things to do when FClientSocket is disconnected go here
end;
//--End;

{Region TServerSocketThread}
//-Begin
constructor TServerSocketThread.Create(CreateSuspended: Boolean);
begin
	inherited Create(True);  
  FreeOnTerminate := True;  
  FSocket := INVALID_SOCKET;
  FListening := False;
  FPort := 0;
end;
            
procedure TServerSocketThread.SetIdle;
var
  FDset: TFDset;
begin
  FD_ZERO(FDSet);
  FD_SET(FSocket, FDSet);
  select(0, @FDset, nil, nil, nil);
end;

procedure TServerSocketThread.Execute;
var
  SockAddrIn: TSockAddrIn;
  SockAddr: TSockAddr;
  TmpSocket: TSocket;
  Len: Integer;
begin
  FSocket := socket(AF_INET, SOCK_STREAM, 0);
  SockAddrIn.sin_family := AF_INET;
  SockAddrIn.sin_addr.s_addr := INADDR_ANY;
  SockAddrIn.sin_port := htons(FPort);

  if bind(FSocket, SockAddrIn, SizeOf(SockAddrIn)) <> 0 then Exit;
  if listen(FSocket, SOMAXCONN) <> 0 then Exit;
  FListening := True;
               
  Len := SizeOf(SockAddr);
  
  repeat
		FClientSocket := TClientSocket.Create;
    FClientSocket.Socket := accept(FSocket, @SockAddr, @Len);
		FClientSocket.SetConnection;
    
    if FClientSocket.Connected then 
    begin
      FClientSocketThread := TClientSocketThread.Create();
      FClientSocketThread.ClientSocket := FClientSocket;
      FClientSocketThread.Resume;
    end;
  until False;
end;

procedure TServerSocketThread.StopListening;
begin
  shutdown(FSocket, SD_BOTH);
  closesocket(FSocket);
	FSocket := INVALID_SOCKET;
  FListening := False;
end;
//--End;

//Don't remove following lines!
initialization
	WSAStartup($0101, WSAData);  

finalization
  WSACleanup();

end.