Is there anyway to make this piece of code more elegant? It's not a nice view, but I can't really see a way to improve the looks of it or shorten it. private void Send(CancellationToken CTSSend) { try { NativeMethods.Rect rc; NativeMethods.GetWindowRect(hwnd, out rc); IntPtr dc1; IntPtr dc2 = NativeMethods.GetWindowDC(hwnd); using (tcp = new TcpClient()) { tcp.NoDelay = true; while (!tcp.Connected && !CTSSend.IsCancellationRequested) { try { if (!tcp.Connected) tcp.Connect(adress); } catch (Exception e) { if (e is SocketException) { Console.WriteLine(e.Message); } else MessageBox.Show(e.Message + " Tcp Connect : Send"); } } using (EncoderParameters JpegParam = new EncoderParameters()) using (JpegParam.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L)) using (Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) using (Graphics g = Graphics.FromImage(bmp)) using (var ms = new MemoryStream()) using (var tcpStream = tcp.GetStream()) while (tcp.Connected && !CTSSend.IsCancellationRequested) { try { dc1 = g.GetHdc(); NativeMethods.BitBlt(dc1, 0, 0, rc.Width, rc.Height, dc2, 0, 0, 13369376); g.ReleaseHdc(dc1); bmp.Save(ms, jpeg, JpegParam); if (bsize != ms.Length) { bsize = ms.Length; tcpStream.Write(BitConverter.GetBytes(bsize), 0, 4); tcpStream.Write(ms.GetBuffer(), 0, (int)bsize); } ms.SetLength(0); } catch (Exception e) { if (e is IOException) { try { connect.Invoke(new Action(() => { connect.Text = "Connect"; })); } catch (InvalidOperationException) { return; } capcon = false; } else MessageBox.Show(e.Message + ": Send Error"); } } } } catch (Exception e) { MessageBox.Show(e.Message + "SEND"); } } I am trying to find a nice way to put TCP reconnecting in another class, but I found a limitation where I have to call `return` in the main class. internal static void TCPReconnect(CancellationToken CTS, TcpClient tcp, IPEndPoint adress) { tcp.NoDelay = true; while (!tcp.Connected && !CTS.IsCancellationRequested) { try { if (!tcp.Connected) tcp.Connect(adress); } catch (Exception e) { if (e is SocketException) { Console.WriteLine(e.Message); } else MessageBox.Show(e.Message + " Tcp Connect : Send"); } } } So, when it fails, it goes out of the loop. I can also make it call `Return`, but it will do the same thing. In the class `Caller` I have to use: FastMethods.TCPReconnect(CTS, tcp, adress); if (CTS.IsCancellationRequested) return; As you can see, I have to recheck for CTS. Is it possible to have it inside the other class somehow? If I use: if (CTS.IsCancellationRequested) return; It will simply return to the main class and continue.