# SocketConnect Connect to the server with timeout control. ``` bool  SocketConnect(    int           socket,               // socket    const string  server,               // connection address    uint          port,                 // connection port    uint          timeout_receive_ms    // connection timeout    ); ``` Parameters socket [in]  Socket handle returned by the [SocketCreate](/en/docs/network/socketcreate) function. When an incorrect handle is passed, the error 5270 (ERR_NETSOCKET_INVALIDHANDLE) is written to [_LastError](/en/docs/predefined/_lasterror). server [in]  Domain name of the server you want to connect to or its IP address. port [in]  Connection port number. timeout_receive_ms [in]  Connection timeout in milliseconds. If connection is not established within that time interval, attempts are stopped. Return Value If connection is successful, return true, otherwise false. Note Connection address should be added to the list of allowed ones on the client terminal side (Tools \ Options \ Expert Advisors). If connection fails, error 5272 (ERR_NETSOCKET_CANNOT_CONNECT) is written to [_LastError](/en/docs/predefined/_lasterror). The function can be called only from Expert Advisors and scripts, as they run in their own execution threads. If calling from an indicator, [GetLastError()](/en/docs/check/getlasterror) returns the error 4014 – "Function is not allowed for call". Example: ``` //+------------------------------------------------------------------+ //|                                                SocketExample.mq5 | //|                        Copyright 2018, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright   "Copyright 2000-2024, MetaQuotes Ltd." #property link        "https://www.mql5.com" #property version     "1.00" #property description "Add Address to the list of allowed ones in the terminal settings to let the example work" #property script_show_inputs   input string Address="www.mql5.com"; input int    Port   =80; bool         ExtTLS =false; //+------------------------------------------------------------------+ //| Send command to the server                                       | //+------------------------------------------------------------------+ bool HTTPSend(int socket,string request)   {    char req[];    int  len=StringToCharArray(request,req)-1;    if(len<0)       return(false); //--- if secure TLS connection is used via the port 443    if(ExtTLS)       return(SocketTlsSend(socket,req,len)==len); //--- if standard TCP connection is used    return(SocketSend(socket,req,len)==len);   } //+------------------------------------------------------------------+ //| Read server response                                             | //+------------------------------------------------------------------+ bool HTTPRecv(int socket,uint timeout)   {    char   rsp[];    string result;    uint   timeout_check=GetTickCount()+timeout; //--- read data from sockets till they are still present but not longer than timeout    do      {       uint len=SocketIsReadable(socket);       if(len)         {          int rsp_len;          //--- various reading commands depending on whether the connection is secure or not          if(ExtTLS)             rsp_len=SocketTlsRead(socket,rsp,len);          else             rsp_len=SocketRead(socket,rsp,len,timeout);          //--- analyze the response          if(rsp_len>0)            {             result+=CharArrayToString(rsp,0,rsp_len);             //--- print only the response header             int header_end=StringFind(result,"\r\n\r\n");             if(header_end>0)               {                Print("HTTP answer header received:");                Print(StringSubstr(result,0,header_end));                return(true);               }            }         }      }    while(GetTickCount()