Reading socket inputstream hangs the whole program

inputstream.copyTo

is also hanging up and in am not able to get method to read the data i passed in output stream ( i also tried to manually copy bytes in a while loop which again hanged up ), while i need just the first line where request URI is present but i don’t want to rely on unreliable hacks

private fun server(switchStatus:AtomicBoolean){
        Log.e("dbg","inside sever function and proceeding to bind")
        val server = ServerSocket(4000,1000, Inet4Address.getByName("0.0.0.0"))
        println("listening to socket")

        runOnUiThread{binding.tvmain.text = "listening to socket"}
        while(true){
            Log.e("dbg","waiting to accept connection")

            if (switchStatus.equals(false)) {break}
            val socket = server.accept()
            val inputStream = socket.getInputStream()

            Log.e("reading","Getting the reader")
            //val reader = inputStream.reader()

            Log.e("reading","reading all data as text")
            // code stuck here

            val data :OutputStream = ByteArrayOutputStream()

            inputStream.copyTo(data, DEFAULT_BUFFER_SIZE)


            Log.e("e"," ")


           // Log.e("output",data.toArray().toString())
            val outputStream = socket.getOutputStream()

            Log.e("writing","writing the output")
            outputStream.write(response.toByteArray())

            Log.e("closing","closing output stream")
            outputStream.flush()
            outputStream.close()


            Log.e("closing","closing socket")
            socket.close()

        }
    }